How to write log messages in a separate file in Python? [duplicate] - python

This question already has answers here:
How to write to a file, using the logging Python module?
(12 answers)
Closed 7 months ago.
Currently I am using the print function to write log messages:
print("...creating new data...")
(I am using the docker image tiangolo/uvicorn-gunicorn-fastapi:python3.7 to run my service).
But in the standard log I can see only the last few messages. I want to archive my log messages over days and month.
How is it possible to write a message in Python in a separate log file?

You can use logging. Here an example

Related

How do I execute a string containing Python, without creating a new file or using "os" module? [duplicate]

This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
How do I execute a string containing Python, without creating a new file, using excec() or using "os" module?
I've seen the similiar question, but the answeres always kind of created some "new file"(for example created something like this), or went out of the current code, but i want it to stay inside the code and execute it.
Example:
string_base64 = "eCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCk="
This b64 code has inside it strings and commands (+ For example imports) and it needs to run in the current file, without using exec() or modules
This Thread and the answers, didn't answer the question!
Buildin exec function seems to be doing exactly what you want:
https://docs.python.org/3/library/functions.html#exec

Print annotations of kubernetes deployment in Python [duplicate]

This question already has answers here:
How to read a Kubernetes Deployment with python kubernetes client
(2 answers)
Closed last year.
I'm writing a python program that prints some kind of data of an existing kubernetes cluster as a first step.
I need to print the annotations of a given deployment. I tried a read the documentation and unfortunately got some lost. Anyone can show me an example?
Here is the example python script,
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md#read_namespaced_deployment
Once you get the api_response, you may have to do,
print(api_response.metadata.annotations)

How to have a subprogramm in another programming language [duplicate]

This question already has answers here:
How do I run a Python script from C#?
(8 answers)
Run a python script from unity, to use its output (text file) in my game later
(4 answers)
Closed 1 year ago.
I want to make a Unity game that gives data to a subprogram written in Python and this subprogram gives back an answer, that is then processed in the C# game.
I don't really know how to approach this.
Can I run python code from C# somehow or do I let two separate programs running, that exchange data somehow?
Both programs also need access to the same database.
You can make a python server using flask or fastAPI and then access it from C#. I am not sure how to get that done with C#(I dont know C# :( ), but there must be some libraries to call servers. Also, you could save the database on the cloud such as mongoDB.

In Python how to map different debug level messages to different files [duplicate]

This question already has an answer here:
using Python logger class to generate multiple logs for different log levels
(1 answer)
Closed 7 years ago.
I need to map the different logging level messages to different files, it is like mapping all info log messages to info.log and all debug messages to debug.log, is that possible in python if yes, how can it be done.
Thanks in Adavance
you can probably achieve this by creating a log handler for each file, then attaching a filter to each handler

Only allow 1 instance of a python script [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: single instance of program
What is the best way to insure that only 1 copy of a python script is running? I am having trouble with python zombies. I tired creating a write lock using open("lock","w"), but python doesn't notify me if the file already has a write lock, it just seems to wait.
Try:
import os
os.open("lock", os.O_CREAT|os.O_EXCL)
The documentation for os.open and its flags.
Your question is similar to this one: What is the best way to open a file for exclusive access in Python?. The answers given there should help you with your issue.
(Use the flag combination portalocker.LOCK_EX!|portalocker.LOCK_NB to return quickly. If the file is locked by another process, your script should get an exception.)

Categories

Resources