17👍
Simple Way of Tagging Log Messages
Do this:
logging.info("TagName: FooBar")
and you message will be tagged! You just need to start all your messages with "TagName: ". And this is of course not very elegant.
Better Solution
Setup your logger:
log = logging.getLogger('name')
address=('log-server',logging.handlers.SYSLOG_UDP_PORT)
facility=logging.handlers.SysLogHandler.LOG_USER
h=logging.handlers.SysLogHandler( address,facility )
f = logging.Formatter('TagName: %(message)s')
h.setFormatter(f)
log.addHandler(h)
And use it:
log.info('FooBar')
7👍
I’m adding this just for the sake of completion, even though @sasha’s answer is absolutely correct.
If you happen to log messages to syslog directly using syslog.syslog
, you can set the tag using the syslog.openlog
function:
import syslog
syslog.openlog('foo')
syslog.syslog('bar')
Going back to the linux shell:
$ tail -f /var/log/syslog
Sep 7 07:01:58 dev-balthazar foo: bar
- Django South Error: AttributeError: 'DateTimeField' object has no attribute 'model'`
- Improving Performance of Django ForeignKey Fields in Admin
- Override create method in django rest generics CreateAPIView
Source:stackexchange.com