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?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
clouvis
  • 567
  • 1
  • 6
  • 18

4 Answers4

11

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

syntonym
  • 7,134
  • 2
  • 32
  • 45
  • 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" – clouvis May 31 '16 at 14:04
  • See [this answer](http://stackoverflow.com/questions/2953834/windows-path-in-python) – syntonym May 31 '16 at 14:13
  • Any chances you can do something like provide an environment variable as a base path? – Glabler Dec 10 '19 at 18:50
  • 1
    @gaber84 Yes, you can use arbitrary python code to construct the string e.g. `filename=os.environ["MY_BASEPATH"] + "myLogFile.log"`. – syntonym Dec 11 '19 at 08:17
  • 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 reply – Glabler Dec 11 '19 at 15:03
  • 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') – Jonathan Jul 04 '20 at 22:19
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')
Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34
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')
AlSub
  • 1,384
  • 1
  • 14
  • 33
Dmitry Erohin
  • 137
  • 1
  • 5
  • 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 path – clouvis May 31 '16 at 14:00
  • in 'D:\\User\\Labo\\Documents\\myPythonScriptFolder\\User\\Labo\\Desktop' | Desktop - its directory or file name? – Dmitry Erohin May 31 '16 at 14:11
0

This is how I soleved it in linux.

# log_cfg.yaml
handlers:
    file_handlers:
        ...
        filename = log//error.log
solst
  • 57
  • 2
  • 6