Many of the modules in Python are very bull X, previously mentioned in the logging module (which is similar to the Java log4j), since a recent script involving network troubleshooting requires log output, which is used in Python's logging module to implement. When the logs are all written to a file, the files become more and more over time, and you can use the Timedrotatingfilehandler method or the Rotatingfilehandler method for processing.
When the log output does not involve rotation, you can customize the output log format through the Logging.basicconfig method, which can refer to my previous blog post---python log module logging. When it comes to the rotation of the date, then use the Logging.basicconfig format processing, not normal output, here in the consultation before the Python Daniel colleagues, the final written code is as follows:
#!/usr/bin/env python
# Coding=utf-8
# site:www.111cn.net
# mail:itybku@139.com
# desc:rotating logfile by times or size
Import re
Import subprocess
Import logging
Import Socket,time
From logging.handlers import Timedrotatingfilehandler
Log_file = "/var/log/ping/ping.log"
#logging. Basicconfig (format= '% (asctime) s% (levelname) s% (message) s ', datefmt= '%y-%m-%d%i:%m:%s ', filemode= ' W ') #for Term print
Logger = Logging.getlogger ()
Logger.setlevel (Logging.info)
FH = Timedrotatingfilehandler (log_file,when= ' M ', interval=1,backupcount=30)
datefmt = '%y-%m-%d%h:%m:%s '
Format_str = '% (asctime) s% (levelname) s% (message) s '
#formatter = logging. Formatter ('% (asctime) s% (levelname) s% (message) s ')
Formatter = logging. Formatter (Format_str, DATEFMT)
Fh.setformatter (Formatter)
Logger.addhandler (FH)
#logging. info (msg)
#hdlr. Flush ()
#----------------------------------------------------------------------
def pinghost (host):
ping = subprocess. Popen (["Ping", "C", "1", Host],stdout = subprocess.) Pipe,stderr = subprocess. PIPE)
Out, error = Ping.communicate ()
If "icmp_seq" in Out:
Icmp_line = Re.findall (R ' \d+\sbytes (. *?) Ms ', out)
Logging.info (' ping ' + host + str (icmp_line))
Else
Logging.info (' ping ' + host + ' fail ')
def tcping (server, port):
' Check If a server accepts connections on a specific TCP port '
Try
Start = Time.time ()
s = socket.socket (socket.af_inet, socket. SOCK_STREAM)
S.connect ((server, port))
S.close ()
#print server + ': ' + str (port) + '/tcp-' + str (port) + ' port is open ' + '-time= ' + str (round (Time.time ()-start) *10 /10) + ' MS '
msg = Server + ': ' + str (port) + '/tcp-' + str (port) + ' port is open ' + '-time= ' + str ((Time.time ()-start) *1000) + ' Ms
Logging.info (msg)
Except Socket.error:
msg = Server + ': ' + str (port) + ' Port not open '
Logging.info (msg)
While 1:
Pinghost (' passport.migu.cn ')
Pinghost (' 112.17.9.72 ')
Tcping (' passport.migu.cn ', 8443)
Tcping (' 112.17.9.72 ', 8443)
#time. Sleep (0.5)
The code has been uploaded to my github.
1. Polling function syntax
The Timedrotatingfilehandler constructor is defined as follows:
Timedrotatingfilehandler (filename [, when [, interval [, Backupcount]]]
FileName is the prefix of the output log file name
When is a string that is defined as follows:
"S": Seconds
"M": Minutes
"H": Hours
"D": Days
"W": Week Day (0=monday)
"Midnight": Roll over at midnight
Interval refers to how many units to wait when the time, logger will automatically rebuild the file, of course, the creation of this file depends on Filename+suffix, if this file with the previous file has the same name, it will automatically overwrite the previous file, So some situations suffix to be defined cannot be repeated because of when.
Backupcount is the number of reserved logs. The default 0 does not automatically delete the log. If you set 10, the library will determine if there is more than this 10 in the creation of the file, and if it exceeds, it will be deleted from the first creation.
2. Poll Usage Example
Rotatingfilehandler (split by file size), Timedrotatingfilehandler (split by time interval) use examples such as the following
HDLR = Logging.handlers.RotatingFileHandler (log_file,maxbytes=1024*1024,backupcount=40) or
HDLR = Logging.handlers.TimedRotatingFileHandler (log_file,when= ' M ', interval=1,backupcount=40)
Where maxbytes specifies the size of each log file, if the file is more than 1024 bits to split the log file, the maximum number of backup files is 40. To Log_file in the directory to view, found in addition to debug.log files, but also more debug.log.1,debug.log.2 and other documents.
3. Logging module Running Process
The logging and handler modules run the following processes:
Import logging
# Create a Logger
Logger = Logging.getlogger (' MyLogger ')
Logger.setlevel (logging. DEBUG)
# Create a handler for writing to the log file
FH = logging. Filehandler (' Test.log ')
Fh.setlevel (logging. DEBUG)
# and create a handler for output to the console
ch = logging. Streamhandler ()
Ch.setlevel (logging. DEBUG)
# defines the output format of the handler
Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')
Fh.setformatter (Formatter)
Ch.setformatter (Formatter)
# Add Handler to Logger
Logger.addhandler (FH)
Logger.addhandler (CH)
# Record a log
Logger.info (' Foorbar ')
4. Logging Level
The Logger.setlevel method can set the level of log display, from highest to lowest order: NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL, if the Looger level is set to info, then logs that are less than the info level are not output, and logs that are greater than or equal to the info level are output.
5. The relationship between father and son of logging
As the following illustration shows, there is a parent-child relationship between the logger logs. The root logger is the logger at the top level.
Python logging
If you do not create a logger instance, call Logging.debug (), Logging.info () logging.warning (), Logging.error (), logging.critical () directly, The logger that is used is the root logger, which can be created automatically and is also single-instance. The root logger instance is obtained by Logging.getlogger () or Logging.getlogger (""). The default level of root logger is logging. WARNING.
The name of the logger can be named to represent the parent-child relationship between logger. Like what:
Parent_logger = Logging.getlogger (' foo ')
Child_logger = Logging.getlogger (' Foo.bar ')
Effective Level:logger has a concept called effective level. If a logger does not show the level, then it uses the level of the father. If the father also does not show the level, the father's father's level, with this push finally reached the root logger, must set the level. Default is logging. WARNING Child loggers Gets the message, distributes the message to its handler processing, and passes it to all ancestors logger processing,
The parent-child test code is as follows:
Import logging
# Set Root Logger
R = Logging.getlogger ()
ch = logging. Streamhandler ()
Ch.setlevel (logging. DEBUG)
Formatter = logging. Formatter ('% (asctime) s-% (levelname) s-% (message) s ')
Ch.setformatter (Formatter)
R.addhandler (CH)
# to create a logger as a father
p = Logging.getlogger (' foo ')
P.setlevel (logging. DEBUG)
ch = logging. Streamhandler ()
Ch.setlevel (logging. DEBUG)
Formatter = logging. Formatter ('% (asctime) s-% (message) s ')
Ch.setformatter (Formatter)
P.addhandler (CH)
# Create a child logger
c = Logging.getlogger (' Foo.bar ')
C.debug (' foo ')
The output is as follows:
2016-03-10 21:04:29,893-foo
2016-03-10 21:04:29,893-debug-foo
The child logger does not have any handler, so does not handle the message. But it forwards the message to its parent logger and root logger. The final output is two logs.
6. Other
Depending on the information you see on the Web, the module uses a scenario where Windows error 32 can be given an error in a multiple-process environment, which requires rewriting the Dorollover function.
The
also involves the configuration section of the logging module, and we can also take effect by specifying a configuration file--Using the Logging.config.fileConfig (' logging.conf ') method to read the custom configuration. You can refer to the official related documents.