here I have a simple code initializing a new logger and just log a simple info message. But the INFO message turns red in console. I thought I should be gray and I want it gray. Debug mode also returns red message in the console.
import logging
logger = logging.getLogger('main')
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter("%(name)s %(asctime)s %(levelname)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info('START EXECUTION')
Even when I use this solution How can I color Python logging output? it doesnt help, info and debug messages remain red.
Related
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 have the following configuration for logger:
def setup_logger(self):
self.logger = logging.getLogger("root")
self.logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
logging.basicConfig(
format='%(asctime)s - %(message)s',
datefmt='%d-%b-%y %H:%M:%S'
)
self.logger.addHandler(ch)
I call the setup_logger() in the constructor(__init()) and I using the logger like
self.logger.info("Getting data from database")
Now the problem is, that it is showing the log message two times in the terminal when I execute the file. One is with the date and time and the other is without time. It is like the following screenshot
I only need to show the log message with the timestamp and don't need the other one. How can I get rid of the duplicate one? I tried to remove the self.logger.setLevel(logging.DEBUG), but then it is not showing any message. Please help.
import logging
logger = logging.getLogger("root")
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(message)s', '%d-%b-%y %H:%M:%S')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("Getting data from database")
logger.info("Generating report for month - January")
I am using python logging module. With this logging module I used to print statements with logging.info func
But problem is that I am not getting stderr messages in the log file. Is there a way I can print stderr with logging module
Following code I am using
import logging
def initialize_logger(output_dir):
'''
This initialise the logger
:param output_dir:
:return:
'''
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# create console handler and set level to info
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
handler = logging.FileHandler(output_dir, "w")
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
In the above code, I am sending only logging statements in the log. It means that I am not getting stderr in the same log file as logging file. Is there a way I can use the same file to send stderr. For example, If my code fails, then along-with logging messages it should also send stderr message.
Right now I don't know why this is happening but seems like my framework has suppressed stderr and stdin messages to print on the console.
Is there a way I can log stderr via logging module?
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.
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"