File: logging_.py

package info (click to toggle)
python-ncclient 0.6.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,448 kB
  • sloc: python: 9,548; xml: 476; makefile: 77; sh: 5
file content (26 lines) | stat: -rw-r--r-- 918 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import logging

class SessionLoggerAdapter(logging.LoggerAdapter):
    """Logger adapter that automatically adds session information to logs."""

    def process(self, msg, kwargs):
        if 'session' not in self.extra or self.extra['session'] is None:
            return msg, kwargs
        session = self.extra['session']
        prefix = ""
        # All Session instances have an id. SSHSessions have a host as well.
        if hasattr(session, 'host'):
            prefix += "host %s " % session.host

        if session.id is not None:
            prefix += "session-id %s" % session.id
        else:
            prefix += "session 0x%x" % id(session)

        # Pass the session information through to the LogRecord itself
        if 'extra' not in kwargs:
            kwargs['extra'] = self.extra
        else:
            kwargs['extra'].update(self.extra)

        return "[%s] %s" % (prefix, msg), kwargs