Change default location log file generated by logger in python
23,313
Try this:
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='path/to/your/directory/testGene.log', filemode='w')
Or
import logging
import os
if not os.path.exists("Logs"):
os.makedirs("Logs")
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='Logs/testGene.log', filemode='w')
Author by
Aman Jaiswal
I am a software developer, I have a knack for picking up new technologies and trying them out. Apart from that, I am an unwinder who loves smashing shuttles, Traveling Places.
Updated on July 17, 2022Comments
-
Aman Jaiswal almost 2 years
I am using logger in my python source code, and want to create logs on specific location but python logging module creates the log files at the default place i.e. from where it is executed.
Is there is any way to change this default location?
below is my configuration
import logging logger = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='testGene.log, filemode='w')
-
Aman Jaiswal almost 6 yearsThis will not work, when you run the python source code it will try to add below two location which will end up with error "No such file or directory" 1: path location of log file 2: location from where script is executed
-
Aman Jaiswal almost 6 yearsThis will not work, when you run the python source code it will try to add below two location which will end up with error "No such file or directory" 1: path location of log file 2: location from where script is executed
-
Hayat almost 6 years@AmanJaiswal It will run if your platform is Linux. And by default, it will add path for the current working directory.
-
Aman Jaiswal almost 6 yearsPlease try to run given solution from multiple places and you will end up creating multiple folder, this will never going to target only one destination folder