-
-
Notifications
You must be signed in to change notification settings - Fork 73
Setting a loglevel for logfiles #57
Comments
I would discourage you from using
Does that work for you like this? |
Hi, Following I have my test script to highlight my issue a bit better: # logtesting.py
#! /usr/bin/env python
# encoding: utf-8
import logging
import logzero
from logzero import logger
# setting up loglevel for console logging
logzero.loglevel(level=logging.INFO, update_custom_handlers=False)
# first logging without logfile
logger.error("Loglevel set to INFO, no logfile.")
logger.debug("DEBUG message") # no console output (expected)
logger.info("INFO message") # console output
logger.warning("WARNING message") # console output
logger.error("ERROR message") # console output
# setting up logfile with same loglevel INFO
logzero.logfile("info.log", formatter=None, mode='a', maxBytes=0, backupCount=0, encoding=None, loglevel=logging.INFO)
# second logging with logfile
logger.error("Loglevel set to INFO, logfile with loglevel set to INFO.")
logger.debug("DEBUG message") # no console output (expected), no logfile output (expected)
logger.info("INFO message") # console output, logfile output
logger.warning("WARNING message") # console output, logfile output
logger.error("ERROR message") # console output, logfile output
# setting up logfile with higher loglevel WARNING
logzero.logfile("warning.log", formatter=None, mode='a', maxBytes=0, backupCount=0, encoding=None, loglevel=logging.WARNING)
# third logging with logfile
logger.error("Loglevel set to INFO, logfile with loglevel set to WARNING.")
logger.debug("DEBUG message") # no console output (expected), no logfile output (expected)
logger.info("INFO message") # console output, no logfile output (expected)
logger.warning("WARNING message") # console output, logfile output
logger.error("ERROR message") # console output, logfile output
# setting up logfile with lower loglevel DEBUG
logzero.logfile("debug.log", formatter=None, mode='a', maxBytes=0, backupCount=0, encoding=None, loglevel=logging.DEBUG)
# fourth logging with logfile
logger.error("Loglevel set to INFO, logfile with loglevel set to DEBUG.")
logger.debug("DEBUG message") # no console output (expected), no logfile output (unexpected!)
logger.info("INFO message") # console output, logfile output
logger.warning("WARNING message") # console output, logfile output
logger.error("ERROR message") # console output, logfile output This produces the following console output:
This output is just like i would have expected. The three logfiles contain the following lines:
Also just like expected.
Also just like expected.
In this one I would have expected a "DEBUG message" . |
Thanks for the example. Yeah, I would also expect the DEBUG message in there. Will take a look tomorrow! |
Thank you, I very much appreciate your support! |
I have to admit that it did not occur to me until now that the cause of this "problem" might lie in the |
Oh, okay. Thanks for the research and information. I'll take a look when I find the time. |
It would also be great if the console could be disable if needed. I do not find any way without changing the code. |
The workaround to this problem is to change log level of Not a nice way, but works for me:
Logzero could easily abstract this bug away in a nice way. |
@alekna However it just seems to defy the purpose of this package to interact with the handlers directly, so I refrained from using one up to now. But I think as far as workarounds go the one you suggested is quite nice, actually, might give it a try. 👍 |
having a |
I sort of had the same issue because I used the setup_logger function, which doesn't seem to work as expected. (The following snippet turned out to work for me) log = logzero.setup_logger(logfile='log.log', level=50, fileLoglevel=10)
log.level = 10 I didn't find any warnings in the documentation that the function shouldn't be used(in this way), so it confused me until I found this reaction of yours. Would it be possible to either fix the behaviour, or mention somewhere that it shouldn't be used like this? |
Thanks for the reports and discussion. I'm taking a fresh look at this now. |
Please take a look at the fix/implementation in #339 - i think that will work. |
First of all I have to admit that I am on some kind of a hiatus from programming in general, I just ran my test file posted above (using the current version |
Thanks for the message. I'm sorry, I've accidentally referenced the wrong PR :) This is the correct one: #338 To test it you'd need to checkout that branch ( Furthermore, wishing you all the best on your hiatus. What are you up to these days? |
Allow lower loglevel for logfiles (fixes #57)
Sorry, for the late answer. |
Hi,
I would like to set the logging level for a logfile to
DEBUG
but leave the logging levelfor the console log at
INFO
, to log debug information to the logfile without cluttering the console.For example by defining a custom logger via
logzero.setup_logger(logfile="custom_logfile.log", level=logging.INFO, fileLoglevel=logging.DEBUG)
.This does not work as I expected. After some testing I came to the conclusion that
the loglevel set via
fileLoglevel
can not be lower (only higher) than the loglevel set vialevel
.If a lower level is set via
fileLoglevel
it gets overwritten bylevel
.Is there a specific reason for this behavior?
The text was updated successfully, but these errors were encountered: