[chirp_devel] [PATCH 01/12] Fix chirp.logger module (#2347)

Zach Welch
Fri Feb 27 17:40:48 PST 2015


On 02/27/2015 03:51 PM, Dan Smith wrote:
>> +        # If we're on Win32 or MacOS, we don't use the console; instead,
>> +        # we create 'debug.log', redirect all output there, and set the
>> +        # console logging handler level to DEBUG.  To test this on Linux,
>> +        # set CHIRP_DEBUG_LOG in the environment.
>> +        console_stream = None
>> +        console_format = '%(levelname)s: %(message)s'
>> +        if hasattr(sys, "frozen") or not os.isatty(0) \
>> +                or os.getenv("CHIRP_DEBUG_LOG"):
>> +            p = platform.get_platform()
>> +            log = file(p.config_file("debug.log"), "w", 0)
>> +            sys.stdout = log
>> +            sys.stderr = log
>> +            console_stream = log
>> +            console_format = self.log_format
>> +            self.early_level = logging.DEBUG
>> +
>> +        self.console = logging.StreamHandler(console_stream)
>> +        self.console.setLevel(self.early_level)
>> +        self.console.setFormatter(logging.Formatter(console_format))
> 
> I think there is a better way to do this:
> 
> class LogWriter(object):
>     def __init__(self):
>         self._log = logging.getLogger('console')
>         self._log.setLevel(logging.DEBUG)
> 
>     def write(self, message):
>         self._log.debug(message)
> 
>  sys.stdout = LogWriter()
>  sys.stderr = sys.stdout
> 
> With that, if I do a "print", it gets logged properly, as module
> "console" at debug level. If we only do that when the console logger is
> actually logging to the debug.log file, then things are simpler and the
> log is consistently formatted. Obviously continuing with converting the
> print statements to log statements still helps so we get the actual
> modules correct in the log statements.

I will give it some thought.  FWIW, I am planning to scour the code to
convert prints to logging calls.  After all, there shouldn't be any
prints in the core chirp code; everything therein should some kind of
logging message, right?

As for the CLI (the only place where normal print calls are expected), I
have already been thinking about enveloping stdout/stderr with a class
that diverts a copy of all printed output to the log file(s).  That will
be necessary to produce a complete log of the CLI's output, which we
will want when debugging users' issues.  However, we do not want printed
output to go through the logger module, as we do not want it adorned.
Does that make sense?

In any event, I agree that there are still logging issues to address,
but they should work themselves out as I get further into those reworks.
-- 
Zach Welch
Mandolin Creek Farm
www.mandolincreekfarm.com
farm: 541-453-4131
cell: 541-740-3410



More information about the chirp_devel mailing list