Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, choose logging files' directory

Tags:

python

logging

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?

Thanks, Matias

like image 517
clouvis Avatar asked May 31 '16 13:05

clouvis


People also ask

What is log Dir in Python?

A Python library for managing logging directories. Source. PyPI.


2 Answers

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

like image 121
syntonym Avatar answered Oct 14 '22 19:10

syntonym


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')
like image 42
AB Abhi Avatar answered Oct 14 '22 18:10

AB Abhi