This article introduces the use of Python log syslog principle explained, the text of the sample code through the introduction of the very detailed, for everyone's learning or work has a certain reference value of learning, the need for friends can refer to the following
The official instructions for syslog are at:
/2/library/#module-syslog
The main way this module works is:
#!/usr/bin/python # -*- coding: utf-8 -*- import syslog ([ident[, logoption[, facility]]]) (priority, message) ()
The information in ident is /bluedon/.
The message for logoption is [4642].
The facility information is the location of the log file, in this article we choose facility = syslog.LOG_USER, that is, the log output is in /var/log/messages.
Source code for:
#!/usr/bin/python # -*- coding: utf-8 -*- import syslog import os if __name__ == '__main__': # /2/library/ # ([ident[, logoption[, facility]]]) # ident filename = (__file__) # logoption # LOG_CONS: If an error occurs while sending the message to the daemon, directly input the relevant message to the relevant message output to the terminal. # LOG_NDELAY: Immediately opens a connection to the system log (normally, a connection to the logging system is opened only if the first log message is generated) # LOG_NOWAIT: does not wait for the creation of possible child processes while logging messages # LOG_ODELAY: similar to the LOG_NDELAY parameter, the connection to the syslog is only created when the syslog function is called # LOG_PID: process number is included in every log message # LOG_PID, LOG_CONS, LOG_NDELAY, LOG_NOWAIT, LOG_PERROR pid = syslog.LOG_PID # facility # LOG_KERN, LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_LPR, LOG_NEWS, LOG_UUCP, LOG_CRON, LOG_SYSLOG, LOG_LOCAL0 to LOG_LOCAL7 filepath = syslog.LOG_USER # Priority # LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG level = syslog.LOG_NOTICE # messages messages = "test start14" # ([ident[, logoption[, facility]]]) (filename, pid, filepath) # (priority, message) (level, messages) # close syslog () # vim var/log/message # tail -f /tmp/
View the results on a different machine:
Write it as class
#!/usr/bin/python # -*- coding: utf-8 -*- import syslog class mysyslog(object): # level # LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG debug = syslog.LOG_DEBUG info = syslog.LOG_INFO notice = syslog.LOG_NOTICE warning = syslog.LOG_WARNING err = syslog.LOG_ERR crit = syslog.LOG_CRIT alert = syslog.LOG_ALERT emerg = syslog.LOG_EMERG # logoption # LOG_PID, LOG_CONS, LOG_NDELAY, LOG_NOWAIT, LOG_PERROR # LOG_CONS: If an error occurs while sending the message to the daemon, directly input the relevant message to the relevant message output to the terminal. # LOG_NDELAY: Immediately opens a connection to the system log (normally, a connection to the logging system is opened only if the first log message is generated) # LOG_NOWAIT: does not wait for the creation of possible child processes while logging messages # LOG_ODELAY: similar to the LOG_NDELAY parameter, the connection to the syslog is only created when the syslog function is called # LOG_PID: process number is included in every log message cons = syslog.LOG_CONS ndelay = syslog.LOG_NDELAY nowait = syslog.LOG_NOWAIT pid = syslog.LOG_PID # facility # LOG_KERN, LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_LPR, LOG_NEWS, LOG_UUCP, LOG_CRON, LOG_SYSLOG, LOG_LOCAL0 to LOG_LOCAL7 # kern = syslog.LOG_KERN # user = syslog.LOG_USER # mail = syslog.LOG_MAIL # daemon = syslog.LOG_DAEMON # auth = syslog.LOG_AUTH # lpr = syslog.LOG_LPR # news = syslog.LOG_NEWS # uucp = syslog.LOG_UUCP # cron = syslog.LOG_CRON # _syslog = syslog.LOG_SYSLOG @classmethod def __init__(self): pass @staticmethod def basicConfig(name, logoption): facility = syslog.LOG_USER (name, logoption, facility) @staticmethod def tosyslog(level, ip, messages): newmessages = "[" + ip + "]" + " " + messages (level, newmessages)
This is the whole content of this article, I hope it will help you to learn more.