Put the handlers only attached to the root_logger and everything should work fine. If your mypkg contains the modules x.py, y.py, a.py, b.py and c.py and you want to log the functions inside x at DEBUG and those inside y at WARNING and those for a/b/c.py at INFO you can do so by setting the levels on the corresponding loggers: mypkg_logger = logging.getLogger('mypkg') Mypkg_logger = logging.getLogger('mypkg') If the user has some own loggers that he wants to use with level DEBUG but he wants to use level INFO for mypkg he can do: root_logger = logging.getLogger() Note that by setting the level and handler on the root logger we are setting the level "globally".
While script2.py will log at level DEBUG to a file: import loggingįile_handler = logging.FileHandler('some_file.log') Mypkg.my_function() # produces output to stderr Then your script.py that uses mypkg and wants to log at level INFO on the console will do: import loggingĬonsole_handler = logging.StreamHandler() No configuration of the logging whatsoever just getLogger(_name_). Module x of mypkg should be something like this: import logging The mypkg should not set the level nor any handler for the logs. Say you have a package mypkg and you want this package to log information while letting the user of the package decide which level of logging to use and where the output should go. I believe you should just follow the standard set up for library code: Logname=sys.argv # could be script1 or script2Īnd get two log files script1.log and script2.log where the division by zero error that occurred in module X is logged in each. ('I am doing a bunch of things in script2 and I will now call X.foo()') # furthermore assume the file handler is set so that the output goes to script2.log. Logname=sys.argv # logname=script2 with the. ('I am doing a bunch of things in script1 and I will now call X.foo()') # furthermore assume the file handler is set so that the output goes to script1.log. Logger=lu.setup_logger(log_name) # assume this does the formatting and sets the filehandlers Logname=sys.argv # logname=script1 with the.
How to import superchecker log files into n1mm logger code#
Here is some code because I am not sure I am making myself clear. Is there a preferred design pattern for doing this? I don't want to rummage through different log files based on the module where the function was called to find my diagnostic information for one of my scripts. Something seems hacky about doing it this way. I also want overnight_script_2.py's call of foo() to log to overnight_script_2.log.Ī potential solution to this problem would be to set up the log file based on looking at the 0th argument to sys.argv which can be mapped to my preferred file. So if overnight_script_1.py calls foo() in module X, I want the log of foo() to go to overnight_script_1.log. I want the logging for module X to write to a log file not dependent on module X, but based on the job that ultimately led to calling functionality in module X. A lot of these jobs use module X let's say. The new changes are shown as highlighted.I like the python logging infrastructure and I want to use it for a number of different overnight jobs that I run. For example, event.getMessage() returns the same String value as the message variable, that’s the message associated with the event. All of the following variables associated with the event are available from the event.
Logging Event is the raw logging event associated with the logging request. Regular logback-classic filters in for SLF4j logback extend the Filter abstract class which actually had a single method, decide() method taking an ILoggingEvent instance as parameter. The following tags can be put within the appender tag to specify a filtering criterion. After some research we found that logback allowed filtering methods. We needed to give some filter condition so that only when the condition was satisfied, the error would be written to the new log file. But this would mean that all the errors would be written to both the files. We had to create one more appender tag and specify a different file. In the above code for SLF4j logback, the File tag within the appender tag, specifies the file to which the errors must be logged.