Coder Social home page Coder Social logo

Comments (16)

alekna avatar alekna commented on August 11, 2024 5

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.

LuckyJosh avatar LuckyJosh commented on August 11, 2024 1

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.

metachris avatar metachris commented on August 11, 2024 1

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.

LuckyJosh avatar LuckyJosh commented on August 11, 2024 1

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.

metachris avatar metachris commented on August 11, 2024

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?

from logzero.

LuckyJosh avatar LuckyJosh commented on August 11, 2024

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.

metachris avatar metachris commented on August 11, 2024

Thanks for the example. Yeah, I would also expect the DEBUG message in there. Will take a look tomorrow!

from logzero.

LuckyJosh avatar LuckyJosh commented on August 11, 2024

Thank you, I very much appreciate your support!

from logzero.

metachris avatar metachris commented on August 11, 2024

Oh, okay. Thanks for the research and information. I'll take a look when I find the time.

from logzero.

mgiaco avatar mgiaco commented on August 11, 2024

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.

LuckyJosh avatar LuckyJosh commented on August 11, 2024

@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.

TheButlah avatar TheButlah commented on August 11, 2024

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.

yaiqsa avatar yaiqsa commented on August 11, 2024

@metachris

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.

metachris avatar metachris commented on August 11, 2024

Please take a look at the fix/implementation in #339 - i think that will work.

from logzero.

metachris avatar metachris commented on August 11, 2024

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.

LuckyJosh avatar LuckyJosh commented on August 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.