1

I have the following .yaml file defining my configurations for the logger I want to use.

version: 1
disable_existing_loggers: False

formatters:
    simple:
        format: "%(asctime)s - %(module)12s - %(name)12s - %(levelname)s - %(message)s"

handlers:
    console:
        class: logging.StreamHandler
        level: DEBUG
        formatter: debug_format
        stream: ext://sys.stdout

    info_file_handler:
        class: logging.handlers.TimedRotatingFileHandler
        level: INFO
        formatter: simple
        interval: 1
        backupCount: 20
        encoding: utf8
        when: midnight
        filename: Log/info.log

    error_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: ERROR
        formatter: error_format
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8
        filename: Log/errors.log

loggers:
    dedalus_logger:
        level: DEBUG
        handlers: [console]
        propagate: false

root:
    level: INFO
    handlers: [console, info_file_handler, error_file_handler]

The problem with this is that my RotatingFileHandler, and TimedRotatingFileHandler will name the files as info.log2, and info.log.2017.10.19,

How can I make it so the names are not set automatically like that but instead have a form that makes more sense, like info.2.log, and info.2017.10.19.log.

Lastly I am defining the filename as Log/info.log if I run my app like this

>>cd Module/src
>>python run.py

It will create the log files where I expect them inside Module/src/Log. If instead I run it like this though

>> python Module/src/run.py

it will try to create the log files in my current directory, which is not what I want, how can I properly configure the logger to fix these two issues?

4
  • For the second issue you can probably specify full address of the log file where you want to log them like - /var/log/my_app/info.log. Or you can use the __name__ variable while setting log file name. Commented Nov 4, 2017 at 14:54
  • If I were to always create them in a standard directory then yes you solution is correct, but I would want it to be in the same directory with the program itself. Commented Nov 4, 2017 at 14:57
  • This should help with your first problem, where you can override the RotatingFileHandler class and customize the log file names as you want. stackoverflow.com/questions/39294275/… Commented Nov 4, 2017 at 15:00
  • You can use something like this - os.path.basename(__file__) or sys.argv[0], as a base reference for log file. Then no matter where your main file is the log file will be created with respect to that file. Commented Nov 4, 2017 at 15:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.