Remote rsyslog server records the logs of python script,rsyslogpython

來源:互聯網
上載者:User

Remote rsyslog server records the logs of python script,rsyslogpython

Ubuntu 14.04.1 LTS

rsyslog server:  10.0.7.77

python client: 10.0.2.122


[rsyslog server]

1.Install rsyslog with apt-get 

sudo apt-get install -y rsyslog 
2.edit /etc/rsyslog.conf ,uncomment this line

$ModLoad immark # provides --MARK-- message capability# provides UDP syslog reception$ModLoad imudp$UDPServerRun 514# provides TCP syslog reception$ModLoad imtcp$InputTCPServerRun 514

   I will custom my log format,so comment this line:

#$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

$template myFormat,"[%$NOW% %TIMESTAMP:8:15% %HOSTNAME%] %programname% %msg%\n"
   set "myFormat" for default tempalte

   ps: You can find more format default attributes at http://www.rsyslog.com/doc/property_replacer.html  

$ActionFileDefaultTemplate myFormat
   create my log file:

$template DynaFile, "/var/log/falls-%$YEAR%-%$MONTH%-%$DAY%.log":programname,contains,"falls"  -?DynaFile
    first line define template named "DynaFile" path is /var/......

    second line means if programname contains "falls" then the logs will be recorded in "DynaFile" path.

3.Edit /etc/default/rsyslog,like this:

RSYSLOGD_OPTIONS=" -r514 -m 0"
4.restart rsyslog server

sudo service rsyslog restart
    check rsyslog network status:

sudo netstat -anpl | grep 514
    If you can see 514 port for tcp and udp ,it's successful.


[client]
creat my test.py

<pre name="code" class="python">#!/usr/bin/env python# encoding: utf-8import loggingimport logging.handlersimport randomdef test():        my_logger = logging.getLogger("falls.com")        my_logger.setLevel(logging.DEBUG)        handler = logging.handlers.SysLogHandler(address = ('10.0.7.77',514))        f = logging.Formatter('falls[10.0.0.1]: %(message)s')        handler.setFormatter(f)        my_logger.addHandler(handler)<span style="white-space:pre"></span>## Produce different log every time with random number for testing.         i = random.randint(1,1000000)        #my_logger.debug('This is a test remote log! %d' % i)        my_logger.info('fuck: This is a test remote log! %d' % i)if __name__ = '__main__':<span style="white-space:pre"></span>test()


when run test.py,it write a log at rsyslog server:/var/log/falls-DATE.log , the content like this:

[2015-02-08 04:17:55 ip-10-0-2-122] falls  fuck: This is a test remote log! 344963



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.