How to set source host address in Python Logging?

Python has a script that analyzes sensor data and events from the number of IPMI servers. Then it sends the graph data to one server, and the error logs to another. Log Server - Syslog-ng + Mysql

So, the task is to store the logs by the owner, but not using the script host.

Code example:

import logging
import logging.handlers

loggerCent = logging.getLogger(prodName + 'Remote')
ce = logging.handlers.SysLogHandler(address=('192.168.1.11', 514), facility='daemon')
formatter = logging.Formatter('%(name)s: %(levelname)s: %(message)s')
loggerCent.setLevel(logging.INFO)

loggerCent.addHandler(ce)

loggerCent.warning('TEST MSG')

So I need to extend the code so that I can tell syslog-ng that the log belongs to a different host. Or some other desire.

Any ideas?

UPD:

So there seems to be a way to use the LogAdapter. But how can this be used:

loggerCent = logging.getLogger(prodName + 'Remote')
ce = logging.handlers.SysLogHandler(address=('192.168.1.11', 514), facility='daemon')
logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)-15s %(name)-5s %(levelname)-8s host: %(host)-15s %(message)s')
loggerCent.addHandler(ce)
loggerCent2 = logging.LoggerAdapter(loggerCent,
                               {'host': '123.231.231.123'})
loggerCent2.warning('TEST MSG')

Searching for a message through TcpDump. I don’t see anything about the host in LoggerAdapter. What am I doing wrong?

UPD2:

, syslog-ng. , Python Logging.

, syslog-ng: CSV Parser

parser ipmimon_log {
        csv-parser(
    columns("LEVEL", "UNIT", "MESSAGE")
    flags(escape-double-char,strip-whitespace)
        delimiters(";")
        quote-pairs('""[](){}')
    );
};

log {
    source(s_net);
    parser(ipmimon_log);
    destination(d_mysql_ipmimon);
};

log {
    source(s_net);
    destination(d_mysql_norm);
    flags(fallback);
};

syslog-ng,

+5
1

-

Formatter Handler. :

loggerCent = logging.getLogger(prodName + 'Remote')
loggerCent.setLevel(logging.DEBUG)
ce = logging.handlers.SysLogHandler(address=('192.168.1.11', 514), facility='daemon')
formatter = logging.Formatter('%(host)s;%(message)s')
ce.setFormatter(formatter)
loggerCent.addHandler(ce)
loggerCent2 = logging.LoggerAdapter(loggerCent, {'host': '123.231.231.123'})
loggerCent2.warning('TEST MSG')

, basicConfig, Handler ( " " ).

0

All Articles