Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set source host address in Python Logging?

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 ;

like image 947
Bulkin Avatar asked Aug 15 '12 18:08

Bulkin


People also ask

What is StreamHandler in Python logging?

The StreamHandler class, located in the core logging package, sends logging output to streams such as sys. stdout, sys. stderr or any file-like object (or, more precisely, any object which supports write() and flush() methods).

What is logging getLogger (__ Name __?

To start logging using the Python logging module, the factory function logging. getLogger(name) is typically executed. The getLogger() function accepts a single argument - the logger's name. It returns a reference to a logger instance with the specified name if provided, or root if not.

What is logging basicConfig in Python?

Python logging basicConfig The basicConfig configures the root logger. It does basic configuration for the logging system by creating a stream handler with a default formatter. The debug , info , warning , error and critical call basicConfig automatically if no handlers are defined for the root logger.


1 Answers

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

like image 50
Silas Ray Avatar answered Oct 06 '22 03:10

Silas Ray