Python logging don t write to file

I'm trying to write a server that logs exceptions both to the console and to a file. I pulled some code off the cookbook. Here it is:

logger = logging.getLogger['server_logger']
logger.setLevel[logging.DEBUG]
# create file handler which logs even debug messages
fh = logging.FileHandler['server.log']
fh.setLevel[logging.DEBUG]
# create console handler with a higher log level
ch = logging.StreamHandler[]
ch.setLevel[logging.ERROR]
# create formatter and add it to the handlers
formatter = logging.Formatter['%[asctime]s - %[levelname]s - %[message]s', datefmt='%Y-%m-%d %H:%M:%S']
ch.setFormatter[formatter]
fh.setFormatter[formatter]
# add the handlers to logger
logger.addHandler[ch]
logger.addHandler[fh]

This code logs perfectly fine to the console, but nothing is logged to the file. The file is created, but nothing is ever written to it. I've tried closing the handler, but that doesn't do anything. Neither does flushing it. I searched the Internet, but apparently I'm the only one with this problem. Does anybody have any idea what the problem is? Thanks for your answers.

asked Apr 9, 2013 at 3:47

thePurpleMonkeythePurpleMonkey

7012 gold badges16 silver badges15 bronze badges

3

Try calling

logger.error['This should go to both console and file']

instead of

logging.error['this will go to the default logger which you have not changed the config of']

answered Apr 9, 2013 at 4:34

3

Try to put the import and the basicConfig at the very beggining of the script. Something like this:

import logging
logging.basicConfig[filename='log.log', level=logging.INFO]
.
.
import ...
import ...

answered Aug 10, 2017 at 12:50

5

Put this

for handler in logging.root.handlers[:]:
    logging.root.removeHandler[handler]

in front of the

logging.basicConfig[...]

see also Logging module not writing to file

answered May 29, 2019 at 18:52

P MoranP Moran

1,4323 gold badges15 silver badges29 bronze badges

1

I know that this question might be a bit too old but I found the above method a bit of an overkill. I ran into a similar issue, I was able to solve it by:

import logging

logging.basicConfig[format = '%[asctime]s %[message]s',
                    datefmt = '%m/%d/%Y %I:%M:%S %p',
                    filename = 'example.log',
                    level=logging.DEBUG]

This will write to example.log all logs that are of level debug or higher.

logging.debug["This is a debug message"] will write This is a debug message to example.log. Level is important for this to work.

answered Oct 16, 2016 at 14:33

Abass SesayAbass Sesay

7839 silver badges18 bronze badges

3

In order to both write to terminal and file you can do like below:

import logging.config

logging.basicConfig[
    level=logging.INFO,
    format="%[asctime]s [%[levelname]s] %[message]s",
    handlers=[
        logging.FileHandler["log_file.log"],
        logging.StreamHandler[]
    ]
]
logger = logging.getLogger[__name__]

usage in the code:

logger.info['message']
logger.error['message']

answered Mar 18, 2021 at 20:26

AmirkhmAmirkhm

7898 silver badges12 bronze badges

If root.handlers is not empty, log file will not be created. We should empty root.handlers before calling basicConfig[] method. source

Snippet:

for handler in logging.root.handlers[:]:
logging.root.removeHandler[handler]

The full code is below:

import logging
##loging
for handler in logging.root.handlers[:]:
    logging.root.removeHandler[handler]

logging.basicConfig[level=logging.DEBUG,
                    format='%[asctime]s %[message]s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename= 'log.txt',
                    filemode='w']

console = logging.StreamHandler[]
console.setLevel[logging.INFO]
# add the handler to the root logger
logging.getLogger[].addHandler[console]
logging.info["\nParameters:"]

for i in range[10]:
    logging.info[i]

logging.info["end!"]

answered Jan 24 at 11:02

Chủ Đề