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

Dan Smith
Fri Feb 27 15:51:42 PST 2015


> +        # 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.

Thoughts?

--Dan


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
Url : http://intrepid.danplanet.com/pipermail/chirp_devel/attachments/20150227/a0737daf/attachment-0001.bin 


More information about the chirp_devel mailing list