I am seeing duplicate entries in the CloudWatch logs despite setting the log.propagate = False, as per the below documentation
https://docs.python.org/2/library/logging.html#logger-objects
def main_logger():
log = logging.getLogger('root')
log.setlevel(logging.INFO)
handler = logging.Streamhandler(stream=sys.stdout)
handler.setlevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(name)s -%(level)s -%(message)s ")
handler.setFormatter(formatter)
log.addHandler(handler)
log.propagate = False
return log
if __ name __ == "__ main __ ":
log = main_logger()
logger = logging.getLogger('root.module')
If i remove log = main_logger(), then few logs are not printed for the existing module itself
Related
I have one "basic" logging configuration that logs to stdout in my project.
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
SERVER_FORMAT = "[%(asctime)s] %(levelname)s [%(name)s:%(filename)s:%(lineno)d] %(message)s"
DATETIME_FORMAT = "%d/%b/%Y %H:%M:%S"
logging.basicConfig(
level=logging.INFO,
format=SERVER_FORMAT,
datefmt=DATETIME_FORMAT,
handlers=[stream_handler])
I have also another logger user_logger that should not print anything. Instead, it should store log entries in a variable.
user_logger = logging.getLogger('user_logger')
user_logger.setLevel(logging.INFO)
log_capture_string = io.StringIO()
log_capture_string_handler = logging.StreamHandler(log_capture_string)
log_capture_string_handler.setLevel(logging.INFO)
log_capture_string_handler.setFormatter(logging.Formatter(USER_FORMAT))
user_logger.handlers = [log_capture_string_handler]
The problem is that when I call:
user_logger.info('This should only be in "log_capture_string"')
it prints it to the console.
Do you know how to avoid that?
You should set user_logger.propagate = False. logging.propagate docs
If this evaluates to false, logging messages are not passed to the handlers of ancestor loggers.
So, your root logger will not send any data to stderr.
This example outputs nothing
import io
import logging
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
SERVER_FORMAT = "[%(asctime)s] %(levelname)s [%(name)s:%(filename)s:%(lineno)d] %(message)s"
DATETIME_FORMAT = "%d/%b/%Y %H:%M:%S"
logging.basicConfig(
level=logging.INFO,
format=SERVER_FORMAT,
datefmt=DATETIME_FORMAT,
handlers=[stream_handler])
user_logger = logging.getLogger('user_logger')
user_logger.propagate = False
user_logger.setLevel(logging.INFO)
log_capture_string = io.StringIO()
log_capture_string_handler = logging.StreamHandler(log_capture_string)
log_capture_string_handler.setLevel(logging.INFO)
log_capture_string_handler.setFormatter(logging.Formatter(SERVER_FORMAT))
user_logger.handlers = [log_capture_string_handler]
user_logger.info('This should only be in "log_capture_string"')
I am trying to log the actions in a function and I have written the following function to log responses to different files based on the type of response i.e. info,error,debug and warning.
logging.basicConfig(filename='indivi_service.log',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' )
def setup_logger(logger_name, log_file, level=logging.DEBUG):
l = logging.getLogger(logger_name)
formatter = logging.Formatter()
fileHandler = logging.FileHandler(log_file)
fileHandler.setFormatter(formatter)
handler = TimedRotatingFileHandler(logger_name,
when="d",
interval=1,
backupCount=100)
l.setLevel(level)
l.addHandler(fileHandler)
l.addHandler(handler)
setup_logger('debug', r'debug')
setup_logger('error', r'error')
setup_logger('warning', r'warning')
setup_logger('info', r'info')
debug = logging.getLogger('debug')
error = logging.getLogger('error')
warning = logging.getLogger('warning')
info = logging.getLogger('info')
class Info(APIHandler):
#gen.coroutine
def post(self):
req = json.loads(self.request.body)
resp, content = client(item_id=req['item_id'])
debug.debug(content)
info.info(hello world)
warning.warn('warning message')
error.error('error message')
The problem that I am facing is that a response is printed twice each time I call a function.
for example:
info.log
hello world
hello world
Can anyone tell me why is it happening like. This is the case with all the log files.
Thanks
try:
import logging
import logging.handlers
logging.basicConfig(filename='indivi_service.log',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' )
def setup_logger(logger_name, log_file, level=logging.DEBUG):
l = logging.getLogger(logger_name)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler = logging.handlers.TimedRotatingFileHandler(str(log_file)+'.log', when="d", interval=1, backupCount=100)
handler.setFormatter(formatter)
l.setLevel(level)
l.addHandler(handler)
setup_logger('debug', r'debug')
setup_logger('error', r'error')
setup_logger('warning', r'warning')
setup_logger('info', r'info')
debug = logging.getLogger('debug')
error = logging.getLogger('error')
warning = logging.getLogger('warning')
info = logging.getLogger('info')
if __name__ == "__main__":
info.info('hello world')
error.info('hello world')
after run this script, file info.log has one 'hello world' and error.log also has only one 'hello world'.
I am using a logger in Python, and I want it to show some messages (not all) with colors in console.
Now I do this:
print('\033[0;41;97m')
logger.info('Colored message')
print('\033[0m')
It works well, but the problem is that there are blank lines above and below the message. I can't do this:
print('\033[0;41;97m', end='')
logger.info('Colored message')
print('\033[0m')
Because this way there is a blank line below the message. And I can't do this neither:
logger.info('\033[0;41;97m' + 'Colored message' + '\033[0m')
Because then, when I open the log file I see weird symbols. Any solution, please?
EDIT
ch = logging.StreamHandler()
fh = logging.handlers.RotatingFileHandler(log_file, 'a', max_bytes, 10)
ch_formatter = logging.Formatter('%(asctime)s - %(levelname)-4s - '
'%(message)s',
datefmt='%Y.%m.%d_%H:%M:%S_%Z')
fh_formatter = logging.Formatter('%(asctime)s - %(levelname)-4s - '
'%(message)s',
datefmt='%Y.%m.%d_%H:%M:%S_%Z')
ch.setFormatter(ch_formatter)
fh.setFormatter(fh_formatter)
logger.addHandler(ch)
logger.addHandler(fh)
UPDATE
OP says I am using a RotatingFileHandler. :)
Define a new FileHandler which escapes ansi color
# file handler for **ansi_escape**
class MyFileHandler(logging.handlers.RotatingFileHandler):
import re
ansi_escape = re.compile(r'\x1b[^m]*m')
def emit(self, record):
record.msg = self.ansi_escape.sub('', record.msg)
logging.handlers.RotatingFileHandler.emit(self, record)
Then add this and StreamHandler to logger
ch = logging.StreamHandler()
fh = MyFileHandler('file.log')
ch_formatter = logging.Formatter('%(asctime)s - %(levelname)-4s - '
'%(message)s',
datefmt='%Y.%m.%d_%H:%M:%S_%Z')
fh_formatter = logging.Formatter('%(asctime)s - %(levelname)-4s - '
'%(message)s',
datefmt='%Y.%m.%d_%H:%M:%S_%Z')
ch.setFormatter(ch_formatter)
fh.setFormatter(fh_formatter)
logger.addHandler(ch)
logger.addHandler(fh)
Define infoc... see below:
logger.infoc = lambda msg: logger.info('\033[0;41;97m' + msg + '\033[0m')
Finally you can archive...
# for colored console but not file
logger.infoc('Colored message')
# for normal
logger.info('Normal message')
EDIT
How about using wrapper for the colored messages.
logger.infoc = lambda msg: logger.info('\033[0;41;97m' + msg + '\033[0m')
logger.infoc('Colored message')
Use basicConfig as
fmt = '\033[0;41;97m %(message)s \033[0m'
logging.basicConfig(level=logging.DEBUG, format=fmt)
logger = logging.getLogger(__file__)
logger.info('hello')
Or
console = logging.StreamHandler()
console.setFormatter(logging.Formatter(fmt))
logger.addHandler(console)
logger.error('hello')
UPDATE
The color code should be in StreamHandler only.
Modify FileHandler like this:
fileh = logging.FileHandler('file.log')
fileh.setFormatter(logging.Formatter('%(message)s')) # remove the color code.
logger.addHandler(fileh)
No more weird symbols in your logfile.
Program A by Python:
LOG_PATH = fdoc_log + "/store_plus.log"
FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(filename=LOG_PATH, filemode = 'w', level=logging.DEBUG, format=FORMAT)
Program B by bash:
mv store_plus.log store_plus.log.bk
The Program A will run in the background and don't stop. When
the Program B delete the file of store_plus.log, the Program A can't write log as well.
If I want the Program A rebuild the store_plus.log, How to solve it ?
Thank you
PS: the way :
f = open(LOG_PATH, "a")
f.close()
It can't work.
An example taken from pymotw-logging and all credit to Doug Hellmann.
import glob
import logging
import logging.handlers
LOG_FILENAME = '/tmp/logging_rotatingfile_example.out'
# Set up a specific logger with our desired output level
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=20, backupCount=5)
my_logger.addHandler(handler)
# Log some messages
for i in range(20):
my_logger.debug('i = %d' % i)
# See what files are created
logfiles = glob.glob('%s*' % LOG_FILENAME)
for filename in logfiles:
print filename
This way is OK by WatchedFileHandler :
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
ch = logging.handlers.WatchedFileHandler('a_log')
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
I need to write a function that logs into a file (using logging module) and also prints the same content on the console at the same time.
What I have is :
def printScreenAndLog(msg):
log = logging.getLogger()
log.info(msg)
now = str(datetime.datetime.now())
print now,"%s" % msg
def main():
options, args = usage()
log = logging.getLogger("CMDR")
log.setLevel(logging.DEBUG)
fh = logging.FileHandler('cmdr.log')
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
log.addHandler(fh)
printScreenAndLog("Testing")
if __name__ == "__main__":
main()
This function should do what you require:
def configure_logging():
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
# log to stdout
logging.basicConfig(level=logging.DEBUG, format=log_format)
# also log to file
formatter = logging.Formatter(log_format)
handler = logging.FileHandler("cmdr.log")
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logging.getLogger('').addHandler(handler)
did you try to set the logging level to info instead of debug?
or use log.debug(msg) in your printScreenAndLog function?