2

I configed my logging in below config file, where I have a "filelog" to write the logging info into a file by using:

flog = logging.getLogger(__file__)
flog.info('write!')

However, I want to specify a different file to log to everytime I run. Now I can only write to C:/record.log

How can I pass in a paramter to indicate which file I want to write to?

[loggers]
keys: root, file

[formatters]
keys: detailed,simple

[handlers]
keys: console,filelog

[formatter_simple]
format: %(asctime)s-%(name)s-%(levelname)s:  %(message)s

[formatter_detailed]
format: %(asctime)s-%(name)s-%(levelname)s-%(lineno)d:  %(message)s

[handler_console]
class: StreamHandler
args: []
formatter: detailed

[handler_filelog]
class: handlers.RotatingFileHandler
args: ('C:/record.log','a', 1000000, 5)
formatter: detailed

[logger_root]
level: DEBUG
handlers: console

[logger_file]
level: INFO
qualname: file
handlers: filelog
Lisa
  • 4,126
  • 12
  • 42
  • 71
  • 1
    Two ways to do that I know of: Remove the file handler from the config file and add it programtically on start up in the code itself. Adding logic to detect if a file exists already. OR create a custom FileHandler class in the code, that is then called in the config file, that has the same logic in it. – CasualDemon Nov 09 '17 at 20:06

1 Answers1

0

An old question, but anyway: You should pass default parameters to the config parser. See SO: https://stackoverflow.com/a/49589760/9153403 and https://docs.python.org/2/library/logging.config.html

fkoh111
  • 1
  • 1