Remote rsyslog Server records the logs of Python script

Source: Internet
Author: User
Tags syslog rsyslog

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

2.edit/etc/rsyslog.conf, uncomment this line

$ModLoad Immark # provides--mark--message capability# provides UDP syslog reception$modload imudp$udpserverrun 514# Prov Ides TCP syslog reception$modload Imtcp$inputtcpserverrun 514

I'll 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":p rogramname,contains, "Falls"  -? Dynafile
First line define template named "Dynafile" path is/var/...

Second line means if ProgramName contains ' falls ' then the logs would 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 the 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 the random number for testing.         i = Random.randint (1,1000000)        #my_logger. Debug (' This was 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 logs 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



Remote rsyslog Server records the logs of Python script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.