Python, choose logging files' directory
Asked Answered
H

4

16

I am using the Python logging library and want to choose the folder where the log files will be written.

For the moment, I made an instance of TimedRotatingFileHandler with the entry parameter filename="myLogFile.log". This way myLogFile.log is created on the same folder than my python script. I want to create it into another folder.

How could I create myLogFile.log into , let's say, the Desktop folder?

Hominy answered 31/5, 2016 at 13:13 Comment(0)
A
11

Simple give a different filename like filename=r"C:\User\Matias\Desktop\myLogFile.log

Anyways answered 31/5, 2016 at 13:25 Comment(6)
Thank you for you answer. While typing TimedRotatingFileHandler(filename="C:User\Matias\Desktop\myLogFile.log) I get an error : " No such file or directory: D:\\User\\Labo\\Documents\\myPythonScriptFolder\\User\\Matias\\Desktop"Hominy
See this answerAnyways
Any chances you can do something like provide an environment variable as a base path?Selfrealization
@gaber84 Yes, you can use arbitrary python code to construct the string e.g. filename=os.environ["MY_BASEPATH"] + "myLogFile.log".Anyways
ok so I have to handle it with my own code, I was naively thinking there was a default env var that the library itself was going to look up, thanks for your replySelfrealization
The 'acepted' answer is fine @syntonym, if you want to statically add a file path each time you run your script. This will answer your question. filename = os.path.abspath(os.path.dirname(file)) logging.basicConfig(filename=os.path.join(filename, 'log_0_0.log'), filemode='w')Status
G
11

Let's say logs directory is in the parent directory of the current directory then you can use below 2 lines, to provide that location irrespective of the underlying os.

import os
log_dir = os.path.join(os.path.normpath(os.getcwd() + os.sep + os.pardir), 'logs')
log_fname = os.path.join(log_dir, 'log_file_name.log')
Gerry answered 13/11, 2018 at 10:33 Comment(0)
P
1

Specify an absolute path when creating instance TimedRotatingFileHandler:

from logging.handlers import TimedRotatingFileHandler
h = TimedRotatingFileHandler('/home/user/Desktop/myLogFile.log')

or

from logging.handlers import TimedRotatingFileHandler
h = TimedRotatingFileHandler('C:\\Users\\user\\Desktop\\myLogFile.log')
Pridgen answered 31/5, 2016 at 13:30 Comment(2)
thanks for your answer. This solution do not seem to work: while typing h=TimedRotatingFileHanler('D:User\Labo\Desktop') I got the error : " No such file or directory: D:\\User\\Labo\\Documents\\myPythonScriptFolder\\User\\Labo\\Desktop" For the constructor, filename is a relative path. I would like to give him an absolute pathHominy
in 'D:\\User\\Labo\\Documents\\myPythonScriptFolder\\User\\Labo\\Desktop' | Desktop - its directory or file name?Pridgen
R
0

This is how I soleved it in linux.

# log_cfg.yaml
handlers:
    file_handlers:
        ...
        filename = log//error.log
Racemic answered 21/11, 2018 at 6:16 Comment(1)
Equals? You sure about that?Rickeyricki

© 2022 - 2024 — McMap. All rights reserved.