Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1820444
  • 博文数量: 473
  • 博客积分: 13997
  • 博客等级: 上将
  • 技术积分: 5953
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-22 11:52
文章分类

全部博文(473)

文章存档

2014年(8)

2013年(38)

2012年(95)

2011年(181)

2010年(151)

分类: LINUX

2012-10-10 13:27:40

大家在写程序时,都喜欢将过程和输出结果以 log 的形式保留下来,供参考。大部分朋友都采取的是 open 方式打开一个文件句柄方式,将其信息 write 进去,今天我学习的是采取 unix 或者类 unix 提供的一个 syslog 服务。在我们的 python 里提供了一个 syslog 的模块,这个模块就是为系统 syslog 程序提供的一个接口。
 
以下我将用通俗的语言和例子来说明下面的函数:
syslog.openlog(ident[, logopt[, facility]])
这个就是初始化 syslog 接口的函数,在这里有一个必须的参数和两个可选的参数。
第一个参数 ,ident
这个就是一个标识字符串,是 log 里每一行中程序的名程 , 如:
syslog.openlog("test.py")
syslog.syslog("The process is test.py")
 
tail -n 1 /var/log/messages
Apr 22 16:26:52 databak test.py: The process is test.py
对上了吧?看明白了吧?
第二个参数 ,logopt
选项名称: LOG_CONS,LOG_NDELAY,LOG_NOWAIT,LOG_PID,LOG_PERROR
大家可以从中选择一个,或者多个 ( 要借助或操作符 ”|”), 如 :
Syslog.openlog(“test.py”,syslog.LOG_PID|syslog.LOG_PERROR)
Syslog.syslog(“The messages print pid and messages print to stderr”)
 
>>> syslog.syslog("The messages print pid and messages print to stderr")
test.py[ 16826 ]: The messages print pid and messages print to stderr
 
[root@databak scripts]# tail -n 1 /var/log/messages
Apr 22 16:33:32 databak test.py[16826]: The messages print pid and messages print to stderr
看到没,它一方面将其直接打印出来,另一方面将日志记录到 messages 中,并且按要且添加了此进程的 pid 号
注: LOG_PERROR 错误除了记录到 syslog 机构中,同时还会在 stderr 打印出来。
第三个参数 ,facility
名称: LOG_AUTH,LOG_CRON,LOG_DAEMON,LOG_KERN,LOG_LOCALx,LOG_LPR,LOG_MAIL,LOG_NEWS,LOG_USER,LOG_UUCP
系统管理员使用这些参数配置信息如何分解到不同的文件和服务中。
在前面,我们一直将信息输出到 messages 文件中,这次,我们要结合 syslog.conf 的配置来将其输出到其他文件了。
修改 syslog.conf ,添加
auth.*                                                 /var/log/python.auth
将认证的信息输出到 python.auth 中,那么我们的程序应该如何写呢?
Syslog.openlog(“test.py”,syslog.LOG_PID,syslog.LOG_AUTH)
Syslog.syslog(“Test Auth!”)
[root@databak scripts]# tail -f /var/log/python.auth
Apr 22 16:43:47 databak test.py[16829]: Test Auth!
 
[root@databak scripts]# tail -n 1 /var/log/messages
Apr 22 16:43:17 databak test.py[16829]: Test Auth!
看到没, messages 和 python.auth 这两个文件里都有了相关的信息了,怎么回事呢?我想让其只输出到 python.auth ,不想输出到 messages 呀?我想是因为我们没有配置 syslog 的优先权,我们的 syslog.syslog 默认的优先权是 LOG_INFO, 自然我们将其内容一方面输出到认证的文件里,一方面又输出到 messages 文件里了。所以,我们需要修改下 syslog.conf 的配置,
*.info;*****;auth.none              /var/log/messages        
大家再试下。。。。
 
这个函数大家明白了吧?接下来,我们看 syslog 函数
在上面的例子中, syslog 函数一直伴随着我们,我相信大家也看到了一点门道了,现在我还是把函数公示给大家吧。
syslog.syslog ( [priority], message )
这里有两个参数,一个可选一个必写。
Message 就不用说了,我的例子都有了,只是要说明下可选参数了。
Priority,
名称:LOG_EMERG,LOG_ALERT,LOG_CRIT,LOG_ERR,LOG_WARNING,LOG_INFO,LOG_DEBUG(默认为LOG_INFO)
我们想将认证错误的信息写到python.err文件中,那么修改syslog.conf
auth.err                                           /var/log/python.err
看程序:
Syslog.openlog(“test.py”,syslog.LOG_PID,syslog.LOG_AUTH)
Syslog.syslog(syslog.LOG_ERR,“Add error information to python.err file”)
 
结果:
[root@databak ~]# tail -f /var/log/python.err
Apr 22 17:05:00 databak test.py[16933]: Add error information to python.err file
 
[root@databak ~]# tail -n 1 /var/log/messages
 
实验成功,那么,我改下优先权看下,看程序:
Syslog.openlog(“test.py”,syslog.LOG_PID,syslog.LOG_AUTH)
Syslog.syslog(syslog.LOG_INFO,“Add info information to python.err file”)
结果:
[root@databak ~]# tail -f /var/log/python.err
[root@databak ~]# tail -n 1 /var/log/messages
 
两个文件都没有,因为你的程序没有符合任合一个(syslog.conf)的要求,所以信息不会写到任何一个文件中的。

This module provides an interface to the Unix syslog library routines. Refer to the Unix manual pages for a detailed description of the syslog facility.

This module wraps the system syslog family of routines. A pure Python library that can speak to a syslog server is available in the module as SysLogHandler.

The module defines the following functions:

syslog.syslog(message) syslog.syslog(priority, message)

Send the string message to the system logger. A trailing newline is added if necessary. Each message is tagged with a priority composed of a facility and a level. The optional priority argument, which defaults to LOG_INFO, determines the message priority. If the facility is not encoded in priority using logical-or (LOG_INFO | LOG_USER), the value given in the call is used.

If has not been called prior to the call to , openlog() will be called with no arguments.

syslog.openlog([ident[, logoption[, facility]]])

Logging options of subsequent calls can be set by calling . will call with no arguments if the log is not currently open.

The optional ident keyword argument is a string which is prepended to every message, and defaults to sys.argv[0] with leading path components stripped. The optional logoption keyword argument (default is 0) is a bit field – see below for possible values to combine. The optional facility keyword argument (default is LOG_USER) sets the default facility for messages which do not have a facility explicitly encoded.

syslog.closelog()

Reset the syslog module values and call the system library closelog().

This causes the module to behave as it does when initially imported. For example, will be called on the first call (if hasn’t already been called), and ident and other parameters are reset to defaults.

syslog.setlogmask(maskpri)

Set the priority mask to maskpri and return the previous mask value. Calls to with a priority level not set in maskpri are ignored. The default is to log all priorities. The function LOG_MASK(pri) calculates the mask for the individual priority pri. The function LOG_UPTO(pri) calculates the mask for all priorities up to and including pri.

The module defines the following constants:

Priority levels (high to low):LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG.Facilities:LOG_KERN, LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_LPR, LOG_NEWS, LOG_UUCP, LOG_CRON, LOG_SYSLOG and LOG_LOCAL0 to LOG_LOCAL7.Log options:LOG_PID, LOG_CONS, LOG_NDELAY, LOG_NOWAIT and LOG_PERROR if defined in .
35.15.1. Examples
35.15.1.1. Simple example

A simple set of examples:

import syslog syslog.syslog('Processing started') if error: syslog.syslog(syslog.LOG_ERR, 'Processing started')

An example of setting some log options, these would include the process ID in logged messages, and write the messages to the destination facility used for mail logging:

syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_MAIL) syslog.syslog('E-mail processing initiated...')

阅读(1179) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~