Alon1980
Alon1980

Reputation: 1267

changing python log level on runtime

In log4net we can change the log level in the xml config file on runtime from DEBUG to ERROR for example and the change take effect immediately.

is there a way to do it in python? i haven't figured out yet how to do it.

In addition, for some reason, when the logging file is set without any folder (see example of the config i use at the bottom - yaml dictionary config file), it fails to rotate files. if i change it to be at any other path other the one that the code is in, it works like a charm and 20 files get created. i will be happy to get any help. thanks!

dictionary config extract example:

  rotatingFile:
        class : logging.handlers.RotatingFileHandler
        formatter:  jajahFormater
        level: DEBUG
        filename: chs.log
        maxBytes: 20
        backupCount: 20

Upvotes: 2

Views: 6225

Answers (2)

Vinay Sajip
Vinay Sajip

Reputation: 99355

You really should have posted two questions.

  1. The .NET infrastructure watches the config file and automatically loads it to get the changes, but Python doesn't - so you would need to code this yourself.
  2. The chs.log file in the code directory may be held open (e.g. in an editor), which would prevent rollover - I can't tell without more information.

Upvotes: 1

RedBaron
RedBaron

Reputation: 4755

From the docs use Logger.setLevel() at logger level and Handler.setLevel() at handler level

import logging

my_logger = logging.getLogger('myloggername') #Assumes you have configured the logger somewhere
my_logger.setLevel(logging.INFO)

Upvotes: 0

Related Questions