Comments (16)
The workaround to this problem is to change log level of StreamHandler
handler directly without touching logzero.logger.level
.
Not a nice way, but works for me:
logzero.logfile('test.log', loglevel=logging.DEBUG)
logzero.logger.handlers[0].level = logging.INFO
Logzero could easily abstract this bug away in a nice way.
from logzero.
I have to admit that it did not occur to me until now that the cause of this "problem" might lie in the logging
library itself. And it seems to be the case as can be seen in these to stackoverflow threads here or here.
The issue seems to be that one handler is not enough for this purpose. You need to create a StreamHandler
and a FileHandler
with their respective logleves and the loglevel of the logger has to be the smaller one of those two.
from logzero.
Thanks for the reports and discussion. I'm taking a fresh look at this now.
Anyone still around and motivated to take a look at a possible approach?
from logzero.
First of all I have to admit that I am on some kind of a hiatus from programming in general,
and I have not looked into this issue in particular for quite some time.
But I still wanted to give my two cents:
I just ran my test file posted above (using the current version 1.5.0
) and the behaviour has not changed.
The logfile debug.log
still does not contain the message at DEBUG
-level.
Furthermore, looking at #339 I dont get the impression that the changes made there could have fixed this issue.
from logzero.
I would discourage you from using setup_logger
. The default logger is already setup, and you can configure it more easily and more versatile:
- Use
logzero.loglevel(level=logging.DEBUG, update_custom_handlers=False)
to setup the general loglevel - Use
logzero.logfile(filename, formatter=None, mode='a', maxBytes=0, backupCount=0, encoding=None, loglevel=None)
to setup the logfile with a specificloglevel
for the file only. This will not get overwritten bylogzero.loglevel(..)
Does that work for you like this?
from logzero.
Hi,
thanks for the fast reply!
I started out with the default logger and thought I might need the custom one to achieve this.
It still does not work the way I would want it to work but maybe there is a reason for this.
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:
[E 171107 17:01:57 logtesting:12] Loglevel set to INFO, no logfile.
[I 171107 17:01:57 logtesting:15] INFO message
[W 171107 17:01:57 logtesting:16] WARNING message
[E 171107 17:01:57 logtesting:17] ERROR message
[E 171107 17:01:57 logtesting:23] Loglevel set to INFO, logfile with loglevel set to INFO.
[I 171107 17:01:57 logtesting:26] INFO message
[W 171107 17:01:57 logtesting:27] WARNING message
[E 171107 17:01:57 logtesting:28] ERROR message
[E 171107 17:01:57 logtesting:33] Loglevel set to INFO, logfile with loglevel set to WARNING.
[I 171107 17:01:57 logtesting:36] INFO message
[W 171107 17:01:57 logtesting:37] WARNING message
[E 171107 17:01:57 logtesting:38] ERROR message
[E 171107 17:01:57 logtesting:44] Loglevel set to INFO, logfile with loglevel set to DEBUG.
[I 171107 17:01:57 logtesting:47] INFO message
[W 171107 17:01:57 logtesting:48] WARNING message
[E 171107 17:01:57 logtesting:49] ERROR message
This output is just like i would have expected.
The three logfiles contain the following lines:
# info.log
[E 171107 17:05:05 logtesting:23] Loglevel set to INFO, logfile with loglevel set to INFO.
[I 171107 17:05:05 logtesting:26] INFO message
[W 171107 17:05:05 logtesting:27] WARNING message
[E 171107 17:05:05 logtesting:28] ERROR message
Also just like expected.
# warning.log
[E 171107 17:05:05 logtesting:33] Loglevel set to INFO, logfile with loglevel set to WARNING.
[W 171107 17:05:05 logtesting:37] WARNING message
[E 171107 17:05:05 logtesting:38] ERROR message
Also just like expected.
#debug.log
[E 171107 17:05:05 logtesting:44] Loglevel set to INFO, logfile with loglevel set to DEBUG.
[I 171107 17:05:05 logtesting:47] INFO message
[W 171107 17:05:05 logtesting:48] WARNING message
[E 171107 17:05:05 logtesting:49] ERROR message
In this one I would have expected a "DEBUG message" .
from logzero.
Thanks for the example. Yeah, I would also expect the DEBUG message in there. Will take a look tomorrow!
from logzero.
Thank you, I very much appreciate your support!
from logzero.
Oh, okay. Thanks for the research and information. I'll take a look when I find the time.
from logzero.
It would also be great if the console could be disable if needed. I do not find any way without changing the code.
from logzero.
@alekna
Yes, you are right, there are definitely workarounds for this.
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. 👍
from logzero.
having a logzero.logstream
to abstract interaction with the StreamHandler
like logzero.logfile
abstracts interaction with the FileHandler
would be really nice. I'm having the same issue where I want the file to have a lower log level than the stream, and I can't do it without the hacky method mentioned by @alekna
from logzero.
I would discourage you from using
setup_logger
. The default logger is already setup, and you can configure it more easily and more versatile:* Use `logzero.loglevel(level=logging.DEBUG, update_custom_handlers=False)` to setup the general loglevel * Use `logzero.logfile(filename, formatter=None, mode='a', maxBytes=0, backupCount=0, encoding=None, loglevel=None)` to setup the logfile with a specific `loglevel` for the file only. This will not get overwritten by `logzero.loglevel(..)`
Does that work for you like this?
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?
from logzero.
Please take a look at the fix/implementation in #339 - i think that will work.
from logzero.
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 (logfile-loglevel
).
Furthermore, wishing you all the best on your hiatus. What are you up to these days?
from logzero.
Sorry, for the late answer.
Thanks for your kind wishes. : )
Im currently in the transition phase from physics/data-analysis to physics/math-education.
from logzero.
Related Issues (20)
- No Color for Critical Log Level HOT 2
- [RFE] - Create directory structure, if it doesn't exist
- Date Format Missing HOT 1
- Use logzero as root_logger. HOT 3
- Usage with multiprocessing and single rotated logfile HOT 4
- log content when subprocess.Popen another Python code file HOT 1
- How to show output logzero's log in pytest
- Is there a way to use use "logging.setLoggerClass" HOT 1
- setup_logger() with level set cannot be changed HOT 1
- Inconsistent output in console and logfile.
- To do & Feature ideas 💡 HOT 3
- [Feature Request] Handling multiple arguments and non-string arguments HOT 2
- Logfile JSON encode HOT 2
- logzero in multiple files using import
- setup_default_logger of para maxBytes, backupCount not used HOT 2
- [Suggestion/Discussion] Support for `logging.getLogger(__name__)` HOT 2
- Json Formatter overridden by _get_json_formatter HOT 1
- color in filelog is in a mess HOT 2
- Function 'logfile' assumes that filename is a string
- VS Code intellisense stops working from Pylance 2022.2.4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logzero.