33

I'm writing some code that uses the python logging system. The idea is that if the LOG doesn't already exist create the log but if it does then get the log and resume logging to that file. Here is my code:

import logging
import os

log_filename='Transactions.log')
if os.path.isfile(log_filename)!=True:
    LOG = logging.getLogger('log_filename')
    LOG.setLevel(logging.DEBUG)
    # create file handler which logs even debug messages
    fh = logging.FileHandler('log_filename')
    fh.setLevel(logging.DEBUG)
    # create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    # create formatter and add it to the handlers
    formatter = logging.Formatter('-->%(asctime)s - %(name)s:%(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    # add the handlers to the logger
    LOG.addHandler(fh)
    LOG.addHandler(ch)
else:
    LOG=logging.getLogger()

I suspect the problem is with my else block but I don't know how to fix. Could anybody shed some light on this situation.

DennisLi
  • 3,915
  • 6
  • 30
  • 66
CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
  • set up your logger once and then import `LOG` from any place you want, no need to check if the log file exists – mic4ael Jan 20 '17 at 13:36

3 Answers3

42

The logging module's FileHandler takes care of that for you. No need for complexity.

The handler takes an optional mode parameter, to specify whether it starts writing or appending data to it.

From the docs:

class logging.FileHandler(filename, mode='a', encoding=None, delay=False)

The specified file is opened and used as the stream for logging. If mode is not specified, 'a' is used.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • 36
    If the directory does not exist, you will still get an exception. Logging is happy to create a new file for you, but not a whole directory structure. – mightypile Apr 07 '18 at 21:03
  • 1
    Which mode should @CiaranWelsh specify in order to create the file if not present? – UserK Jun 05 '18 at 12:41
  • 4
    @UserK if file is not present, either of `a` or `w` would be fine. `a` would append to the same file in next run, whereas `w` mode will delete the old file and create an entirely new one. Also read what @mightypile commented above. – hjpotter92 Jun 05 '18 at 13:28
  • On Python 2.7, it doesn't seem to work. `FileHandler` can't find the path and it returns error unless you explicitly make a path using `os.makedirs()` – user8491363 Jul 30 '20 at 01:36
32

For anyone who was trying to create a new directory structure like logs/mylogfile.log, as @mightypile mentioned, FileHandler will not create a new directory structure for you. I used os.makedirs to ensure the directory structure.

import os
import logging

log_filename = "logs/output.log"
os.makedirs(os.path.dirname(log_filename), exist_ok=True)
file_handler = logging.FileHandler(output_filename, mode="w", encoding=None, delay=False)
lotrgollum87
  • 320
  • 3
  • 7
0

When you run

LOG = logging.getLogger('log_filename')

for the first time a global variable is created. Hence, you could also add the following code to the script above:

global LOG
if LOG is not None:
    print("found logger !")
else:
    ("no global variable logger found")
Angelo
  • 1,594
  • 5
  • 17
  • 50