Comments (6)
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.
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.
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.
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.
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.
Great! I will release the new version now.
from logging_tree.
Related Issues (10)
- Exception if formatter doesn't have a _fmt attribute HOT 2
- Overridden parent loggers HOT 7
- document what broken .parent means, and how to fix it HOT 5
- Failing tests on python-3.3 HOT 3
- Python 3.8 support HOT 1
- This should be part of corelibrary logging HOT 3
- Tag the source HOT 1
- FR: Show if the logging level is delegated or not HOT 5
- Adding documentation HOT 6
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 logging_tree.