Reputation: 1221
I have the following config in my Python 3.6 application:
config = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {
"format": ("[%(asctime)s] %(levelname)s - %(threadName)s - %(name)s - %(message)s")
},
},
"handlers": {
"default": {
"class": "logging.StreamHandler",
"formatter": "standard",
"level": DEFAULT_LOG_LEVEL,
},
"fh": {
"class": "logging.handlers.RotatingFileHandler",
"formatter": "standard",
"level": FILE_LOG_LEVEL,
"filename": "myfile.log",
"maxBytes": 1024*1000, # 1 MB
"backupCount": 20
}
},
"loggers": {
'': {
"handlers": ["default", "fh"],
'level': DEFAULT_LOG_LEVEL,
'propagate': True
}
}
}
This currently works; however, it saves all the .log files to the current working directory (where the executable is on Windows).
What I'd like to do is have it save this to the "logs" directory which is in the same level as the executable itself and already existing.
My thought was to modify the "filename" property int he log configuration, but I'm not sure how that impacts my script files that use:
logger = logging.getLogger(__name__)
Would that need to be modified as well to get it to log to the correct location?
Desired Structure:
Upvotes: 2
Views: 795
Reputation: 1221
My initial thought seems do what I need after testing. I simply put the directory name in front of the file name in the config's "filename" property value. No modification was needed to the script's logger line.
Upvotes: 1