Why is python ignoring my formatter in the console? - python

am working on a Python app that implements logger features
Here is my code which is taken form the official site:
import logging
#create logger
logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG)
#create console handler and set level to debug
fh = logging.RotatingFileHandler(MyFile, etc)
fh.setLevel(logging.DEBUG)
#create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s -
%(message)s")
#add formatter to fh
fh.setFormatter(formatter)
#add fh to logger
logger.addHandler(fh)
#"application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")
and here is the output in the File: which is perfect
2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message
2005-03-19 15:10:26,620 - simple_example - INFO - info message
2005-03-19 15:10:26,695 - simple_example - WARNING - warn message
2005-03-19 15:10:26,697 - simple_example - ERROR - error message
2005-03-19 15:10:26,773 - simple_example - CRITICAL - critical message
but here is the output in the terminal:
DEBUG: debug message
INFO: info message
WARNING: warn message
ERROR: error message
CRITICAL: critical message
that drives me crazy since i can not get to see the timestamp in the console...
I have tried too creating another handler:
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
and the adding it to logger...
but the only thing I get is that the logger in printing twice every msg in the console... one perfectly ok and the wrong one as I explained at begin

The console output can be adjusted separately using StreamHandler, for which you have to add the formatter as well:
#create console handler and set level to debug
fh = logging.RotatingFileHandler(MyFile, etc)
fh.setLevel(logging.DEBUG)
#create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s -
%(message)s")
#add formatter to fh
fh.setFormatter(formatter)
#add fh to logger
logger.addHandler(fh)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)

Here is what I use for logging in a file and in the console:
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s\t%(message)s', datefmt='%m-%d %H:%M', filename=logfile, filemode='a')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
# set a format which is simpler for console use
formatter = logging.Formatter('%(asctime)s %(levelname)s\t%(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
# Now, we can log to the root logger, or any other logger. First the root...
logging.info('LOGGING PATH: %s', logfile)
Then you can set the same output for both the file and the console.

A initialization in the basic configuration on the logger is necessary for that:
logging.basicConfig(streamä0sys.stdout, level=logging.INFO, format=myFormat)
where
myFormat= "%(asctime)-15s %(clientip)s %(user)-8s %(message)s"

Related

Cannot log to stream in Python

I want to log to stream of io.StringIO, but end up with empty stream. Here is my code:
import logging, io
log_handler = logging.StreamHandler(stream)
log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log_handler.setFormatter(log_format)
log_handler.setLevel(logging.INFO)
logger.addHandler(log_handler)
logger.info("This is test info log")
print(stream.getvalue())
What am I doing wrong?
UPDATE
It seems to be working if I replace level with "Warning" like this
log_handler.setLevel(logging.WARNING)
logger.warning("This is test info log")
It also prints into console for some reason
Configure the root logger: logging.basicConfig(level=logging.DEBUG).
Then you can set log_handler.setLevel(logging.INFO) to whatever level you wish, it will capture logs to stream accordingly.

I am getting this error related to logging

No handlers could be found for logger reports_wrapper
if logging.getLogger().hasHandlers():
logger = logging.getLogger(__name__)
else:
logger = logging.getLogger("reports_wrapper")
streamHandler = logging.StreamHandler()
streamHandler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)
then when i am trying to do
logging like logging.info("abc") , i am getting the error that no handlers could be found for reports_wrapper

How to create an error log file after an error in Python?

I'm writing a Python script and already use the logging library to log errors in my script, but I would like to create this error log file only after the error has appeared, can that be done?
Here's the code I have:
import logging
logger = logging.getLogger('my_app')
logger.setLevel(logging.INFO)
fh = logging.FileHandler("log.txt")
fh.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
try:
# my code
except:
logger.exception("my message")
import logging
formatter = logging.Formatter('%(asctime)s - %(message)s', datefmt='%d-%m-%y %H:%M:%S')
def setup_logger(name, log_file, setLevel):
"""To setup as many loggers as you want"""
handler = logging.FileHandler(log_file)
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level=setLevel)
logger.addHandler(handler)
return logger
error_log = setup_logger('error_logger', 'error.log', logging.ERROR)
err_log.error("ERROR YOU WANT TO PUT")
You can have the debug logger as logging.DEBUG and create as many logger with the function

Python logging levels with rsyslog

I am trying to filter levels in rsyslog.d/conf files from the python logger but nothing is getting filtered.
import logging
import logging.handlers
if __name__ == "__main__":
logger = logging.getLogger()
logger.setLevel(logging.INFO)
fh = logging.handlers.RotatingFileHandler('./logtest.log', maxBytes=10240, backupCount=5)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('INFO')
logger.error('ERROR')
In my conf file I have:
*.=info -/var/log/info.log
But the info logs are not being logged to that file, any ideas why?
This has been solved. The issue did lie with syslog at all, I had issues in my program where is was not logging the levels correctly to the facility.
Aka: they were all being logged as warnings.

Is there a way to change the filemode for a logger object that is not configured using basicConfig?

If I create a logger object by using logger = logging.getLogger("Name") I am unable to change the filemode from append('a') to write ('w'). I can if I use the root logger with basicConfig, but then I get a lot of system debug messages being logged when all I want is my own messages beginning at the DEBUG level.
I am hoping to either (1) change the filemode for my own logger object to 'w'
or (2) add a filter to the root logger. Is it even possible to filter out these debug messages from the root logger?
def create_log():
# create logger for "Sample App"
logger = logging.getLogger('automated_testing')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('results.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler(stream=sys.stdout)
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter('[%(asctime)s] %(levelname)8s --- %(message)s ' +
'(%(filename)s:%(lineno)s)',datefmt='%Y-%m-%d %H:%M:%S')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(ch)
logger.addHandler(fh)
return logger
Something like:
import sys
import logging
def create_logger():
# create logger for "Sample App"
logger = logging.getLogger('automated_testing')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('results.log', mode='w')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler(stream=sys.stdout)
ch.setLevel(logging.INFO)
# create formatter and add it to the handlers
formatter = logging.Formatter('[%(asctime)s] %(levelname)8s --- %(message)s ' +
'(%(filename)s:%(lineno)s)',datefmt='%Y-%m-%d %H:%M:%S')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(ch)
logger.addHandler(fh)
return logger
logger = create_logger()
logger.log(logging.NOTSET, "NOTSET Message - 0")
logger.log(logging.DEBUG, "DEBUG Message - 10")
logger.log(logging.INFO, "INFO Message - 20")
logger.log(logging.WARNING, "WARNING Message - 30")
logger.log(logging.CRITICAL, "CRITICAL Message - 40")
Prints to stdout:
[2015-03-16 17:51:08] INFO --- INFO Message - 20 (temp3.py:34)
[2015-03-16 17:51:08] WARNING --- WARNING Message - 30 (temp3.py:35)
[2015-03-16 17:51:08] CRITICAL --- CRITICAL Message - 40 (temp3.py:36)
Writes (not appends) to results.log:
[2015-03-16 17:51:08] DEBUG --- DEBUG Message - 10 (temp3.py:33)
[2015-03-16 17:51:08] INFO --- INFO Message - 20 (temp3.py:34)
[2015-03-16 17:51:08] WARNING --- WARNING Message - 30 (temp3.py:35)
[2015-03-16 17:51:08] CRITICAL --- CRITICAL Message - 40 (temp3.py:36)
DEBUG+ are logged in results.txt while only INFO+ are send to stdout.
Note that the NOTSET log entry is passed up to the root logger then, since you don't have any handlers on the root logger, discarded.

Categories