Question

There is a script, written in Python, which parse sensors data and events from number of servers by IPMI. Then it sends graph data to one server and error logs to the other. Logging server is Syslog-ng+Mysql

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

Some 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 I could tell to syslog-ng, that the log belongs to the other host. Or some other desigion.

Any ideas?

UPD:

So it looks like there is the way to use LogAdapter. But how can use it:

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')

Looking for the message through TcpDump I see nothing about host in LoggerAdapter What am I doing wrong?

UPD2:

Well, I did't find the way to send host to syslog-ng. Though it is possible to send first host in chain, but I really can't find the way to sent it through Python Logging.

Anyway, I made parser in 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);
};

Then I send logs to the syslog-ng delimited by ;

Was it helpful?

Solution

edit-rewrite

You are missing the critical step of actually adding your Formatter to your Handler. Try:

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')

Note that you won't be running basicConfig any more, so you will be responsible for attaching a Handler and setting a log level for the root logger yourself (or you will get 'no handler' errors).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top