-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mulitple Log FIles #31
Comments
Could you share more about your program setup? Are you using multiprocessing, threading, or two separate applications? Basically, the log destination is defined by a log handler. For one application to log into different files, you add a handler for each file and set the condition for what log when to what file. That being said, for different applications that run simultaneously, setting the logging port differently for each application is sufficient. |
Hi @Dragon2fly My program is single process, multi threaded. It is a Flask server that logs api requests and current state. I want to start another thread that logs to a different file for just task progress tracking. |
Maybe if there is some function that acts like setup_logging but returns a new logger, that would be easy to use. |
Your case is simple, logging with threading is fully supported by default. Instead of doing the setup in code, you will do it in the log config file. The code just uses it. (1) Copy the log config file (either json or yaml) and paste it into your project folder. (2) Create a new handler that logs to the file (similar to "task_progress_handler": {
"class": "logging.handlers.TimedRotatingFileHandler",
"level": "DEBUG",
"formatter": "simple",
"filename": "logs/task_progress.txt",
"backupCount": 15,
"encoding": "utf8",
"when": "midnight",
"delay": true
}, You could customize all the parameters as it is described in TimedRotatingFileHandler. (3) Create a new logger that uses the previously added handler "progress_logger": {
"level": "DEBUG",
"handlers": ["console", "task_progress_handler"],
"propagate": false
} Notice that I also add the (4) In the from logger_tt import setup_logging
setup_logging(config_path="log_config.json") This assumes the (5) Use the new logger in the code. from logging import getLogger
logger = getLogger(__name__) You will add one line for the new logger and start using it where applicable from logging import getLogger
logger = getLogger(__name__) # general logger
progress_logger = getLogger("task_progress_handler") # progress related logger
def my_task(*arg, **kwarg):
logger.info("My task started")
...
progress_logger(f"processed item {i}th")
...
logger.info("My task finished") That is it. Hope it helps. |
Is there a recommended way to log to multiple files? I would like to create a second logger that outputs to a new log file. I do not want the new logger to destroy or modify the existing logger that has been customized using setup_logging.
Thanks in advance.
The text was updated successfully, but these errors were encountered: