From logging to files to a better solution: syslog, Sentry, Logstash, ....

jmp jeanmichel at sequans.com
Fri Sep 11 05:02:54 EDT 2015


On 09/11/2015 09:22 AM, Thomas Güttler wrote:
>
> I want INFO to be logged and stored on the remote host.
> Therefore I must not filter INFO messages.
>
> I don't want to pull INFO messages over the VPN.
>
> Ergo, the filtering at Python level does not help in my use case.
> Or I am missing something.

Probably,

Your logger should have

   * a remote host handler
   * and a VPN handler.

You can set filters and log levels separately for each handler.
More info here
https://docs.python.org/2/library/logging.html#handler-objects
https://docs.python.org/2/howto/logging-cookbook.html#logging-to-multiple-destinations

Something like (python 2.7)

import logging

logCfg = {
     'remote':(
         logging.StreamHandler(),
         logging.Formatter('Remote - %(levelname)s - %(message)s'),
         logging.INFO,
         ),
     'vpn':(
         logging.StreamHandler(),
         logging.Formatter('VPN - %(levelname)s - %(message)s'),
         logging.ERROR,
         ),
}

log = logging.getLogger()
log.setLevel(logging.DEBUG)

for handler, formatter, level in logCfg.itervalues():
     handler.setFormatter(formatter)
     handler.setLevel(level)
     log.addHandler(handler)

log.info('This is an info')
log.error('This is error')


and the result:

Remote - INFO - This is an info
VPN - ERROR - This is error
Remote - ERROR - This is error


JM





More information about the Python-list mailing list