In my Python program, I have the following code:
def main():
path = os.path.dirname(os.path.realpath(__file__))
...
loggingConf = open('{0}/configs/logging.yml'.format(path), 'r')
logging.config.dictConfig(yaml.load(loggingConf))
loggingConf.close()
logger = logging.getLogger(LOGGER)
...
and this is my logging.yml configuration file:
version: 1
formatters:
default:
format: '%(asctime)s %(levelname)s %(name)s %(message)s'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: default
stream: ext://sys.stdout
file:
class : logging.FileHandler
formatter: default
filename: bot.log
loggers:
cloaked_chatter:
level: DEBUG
handlers: [console, file]
propagate: no
The problem is that the bot.log file is created where the program is running. I want it to always be created in the project folder, i.e. In the same folder as my Python program.
For example, running the program with ./bot.pywill create a log file in the same folder. But running with python3 path/bot.pywill create a log file above the Python program in the file hierarchy.
How can I write the file name in the configuration file to solve this problem? Or do I need to write my own handler? If so, how? Or is it impossible to solve with dictConfig?