Giter Club home page Giter Club logo

Comments (6)

brandon-rhodes avatar brandon-rhodes commented on August 23, 2024

Could you supply me with a quick snippet of code that sets a handler logging level, together with how you would include it in the output? Thanks!

from logging_tree.

mgrandi avatar mgrandi commented on August 23, 2024

Example code on how handlers / levels work:

import logging
import logging.handlers
import io

customStream = io.StringIO()
customStream2 = io.StringIO()

formatter = logging.Formatter(fmt='%(asctime)s - %(message)s')

rootLogger = logging.getLogger("")
rootLogger.setLevel("DEBUG") # make it so the logger itself lets all messages through

handlerOne = logging.StreamHandler(customStream)
handlerOne.setFormatter(formatter)
handlerOne.setLevel("DEBUG") # this handler will only allow messages with levels >= DEBUG

handlerTwo = logging.StreamHandler(customStream2)
handlerTwo.setFormatter(formatter)
handlerTwo.setLevel("ERROR") # this handler will only allow messages with levels >= ERROR

rootLogger.addHandler(handlerOne)
rootLogger.addHandler(handlerTwo)

rootLogger.debug("this is a DEBUG message")
rootLogger.info("this is an INFO message")
rootLogger.warning("this is a WARNING")
rootLogger.error("this is an ERROR")
rootLogger.critical("this is a CRITICAL message")

print("customStream output (DEBUG): \n{}".format(customStream.getvalue()))

print("customStream2 output(ERROR): \n{}".format(customStream2.getvalue()))

and the output:

Corvidae:Desktop markgrandi$ python3 testHandler.py
customStream output (DEBUG):
2014-12-23 15:46:31,823 - this is a DEBUG message
2014-12-23 15:46:31,824 - this is an INFO message
2014-12-23 15:46:31,824 - this is a WARNING
2014-12-23 15:46:31,824 - this is an ERROR
2014-12-23 15:46:31,824 - this is a CRITICAL message

customStream2 output(ERROR):
2014-12-23 15:46:31,824 - this is an ERROR
2014-12-23 15:46:31,824 - this is a CRITICAL message

from logging_tree.

mgrandi avatar mgrandi commented on August 23, 2024

And as for how to output stuff like this, you could just follow the tree structure you already have. I would also like to see formatters included, to see the whole logging picture =)

<--""
   Level DEBUG
   Handler Stream (DEBUG) <_io.StringIO object at 0x10208c678>
       Formatter(fmt='%(asctime)s - %(message)s', datestr=ISO8601/Default)
   Handler Stream (ERROR) <_io.StringIO object at 0x102091e58>
       Formatter(fmt='%(asctime)s - %(message)s', datestr=ISO8601/Default)

   |
   o<--"test"
       Level INFO
       |
       o<--"test.testing"
           Level WARNING

from logging_tree.

brandon-rhodes avatar brandon-rhodes commented on August 23, 2024

Thank you both for your ideas! I would not have thought of these ideas, and it is requests like yours that make a little tool like this much more complete with other people try it out.

If either of you could try re-installing with the following command, you can let me know whether the new features work and are worth a new release of logging tree. Thanks!

$ pip install https://github.com/brandon-rhodes/logging_tree/archive/master.zip

from logging_tree.

mgrandi avatar mgrandi commented on August 23, 2024

It looks great, thanks for such the fast response =)

<--""
   Level DEBUG
   Handler Stream <_io.StringIO object at 0x10208c678>
     Level DEBUG
     Formatter fmt='%(asctime)s - %(message)s' datefmt=None
   Handler Stream <_io.StringIO object at 0x102091e58>
     Level ERROR
     Formatter fmt='%(asctime)s - %(message)s' datefmt=None
   |
   o<--"testing"
       Level NOTSET so inherits level DEBUG
       Handler Stream <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>

from logging_tree.

brandon-rhodes avatar brandon-rhodes commented on August 23, 2024

Great! I will release the new version now.

from logging_tree.

Related Issues (10)

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.