本篇文章主要介绍一下,如何使用 python 来编写属于自己的日志模块
1. 使用 python 来实现多线程并发写日志文件
-
from threading import Thread
-
-
import logging
-
from logging import handlers
-
from logging.handlers import RotatingFileHandler
-
from logging import Formatter
-
-
from time import sleep
-
import time
-
-
-
LOG_LEVEL = {
-
'default' :logging.DEBUG,
-
'debug' :logging.DEBUG,
-
'info' :logging.INFO,
-
'warning' :logging.WARNING,
-
'error' :logging.ERROR,
-
'critical' :logging.CRITICAL }
-
-
LOG_FILENAME = ''
-
LOG_BACKUP_COUNTER = 3
-
LOG_TYPE = 'default'
-
LOG_MAX_SIZE = 1024*1024*4 # bits = 0.5MB of each log file
-
-
-
def InitLogger(log_file_name, logger):
-
str_time = time.strftime("%Y%m%d_%H%M%S")
-
LOG_FILENAME = str_time+'_'+log_file_name
-
#print(LOG_FILENAME)
-
-
handler = RotatingFileHandler( LOG_FILENAME,maxBytes = LOG_MAX_SIZE, backupCount = LOG_BACKUP_COUNTER)
-
-
# method indefault
-
# handler = FileHandler( LOG_FILENAME)
-
-
# set log file output message format
-
fmt = '[ %(asctime)s ]--[ %(filename)s:%(lineno)s ]--[%(name)s]--[%(levelname)s]--[ %(message)s ]'
-
-
formatter = Formatter (fmt)
-
-
handler.setFormatter( formatter )
-
-
logger.addHandler(handler)
-
-
logger.setLevel(LOG_LEVEL.get(LOG_TYPE.lower()))
-
-
-
# after set every parameters , return logger
-
return logger
-
-
-
#define a thread writer class
-
class log_writter(Thread):
-
def __init__(self,threadName,file_name):
-
-
self.log_file_name = file_name
-
-
Thread.__init__(self,name = threadName)
-
-
logger = logging.getLogger(self.log_file_name)
-
-
self.logger = InitLogger(self.log_file_name,logger)
-
-
-
-
def run(self):
-
-
global i
-
-
self.logger.info(self.log_file_name)
-
-
while i in range(10):
-
print '**** log writer '+self.log_file_name+' test ******'
-
-
self.logger.info ('hello aimer')
-
-
self.logger.debug ('hello kokia')
-
-
self.logger.warning ('hello kylin-zhang')
-
-
i += 1
-
-
time.sleep (3)
-
-
print('end of log file test '+ self.log_file_name )
-
-
-
-
-
i = 0
-
-
threadCounter = 2400
-
-
for j in range(threadCounter):
-
-
# s is used for thread and log file no.
-
s = 'thread'
-
s +='_'+str(j)
-
-
print ( s )
-
-
log_file = str(j)+'_mylog.log'
-
my_logger = log_writter( s , log_file)
-
my_logger.start ()
运行方法:
python myLogger.py
2. 将编写的日志模块简单修改之后应用在实际项目代码中
在这个地方会涉及到这样一个问题:
如何将一个路径下面的 .py 文件中定义的方法引入到另一个 .py 文件中
解决方法很简单:
举例来说,我们要在 node.py 中使用 myLogger.py 中定义并且实现的 InitLogger 这个方法
|------ myLogger.py
|------ node_id.py
首先在需要引入的 .py 文件(myLogger.py)路径下面创建一个名为 __init__.py 的文件即可(空文件就可以,
python 会将含有该文件的文件夹当做是一个模块--->module)
|--- myLogger.py
|--- __init__.py (刚刚创建)
|--- node_id.py
然后在 node_in.py 文件中的开头部分添加引入 myLogger.py 以及引入文件中某个方法的声明语句 :
from myLogger import InitLogger
即可
如果要将日志模块添加到代码中,需要对刚刚编写的 myLogger.py 文件中多线程类和后面调用多线程来写日志的代码进行注释
使用 ''''''' 便可以实现注释的功能
// another version of myLogger.py
// myLogger.py
-
from threading import Thread
-
-
import logging
-
from logging import handlers
-
from logging.handlers import RotatingFileHandler
-
from logging import Formatter
-
-
from time import sleep
-
import time
-
-
-
LOG_LEVEL = {
-
'default' :logging.DEBUG,
-
'debug' :logging.DEBUG,
-
'info' :logging.INFO,
-
'warning' :logging.WARNING,
-
'error' :logging.ERROR,
-
'critical' :logging.CRITICAL }
-
-
LOG_FILENAME = ''
-
LOG_BACKUP_COUNTER = 3
-
LOG_TYPE = 'default'
-
LOG_MAX_SIZE = 1024*1024*32 # bits = 4MB
-
-
-
def InitLogger(log_file_name, logger):
-
str_time = time.strftime("%Y%m%d_%H%M%S")
-
LOG_FILENAME = str_time+'_'+log_file_name
-
print(LOG_FILENAME)
-
-
handler = RotatingFileHandler( LOG_FILENAME,maxBytes = LOG_MAX_SIZE, backupCount = LOG_BACKUP_COUNTER)
-
-
# method indefault
-
# handler = FileHandler( LOG_FILENAME)
-
-
# set log file output message format
-
fmt = '[ %(asctime)s ]--[ %(filename)s:%(lineno)s ]--[%(name)s]--[%(levelname)s]--[ %(message)s ]'
-
-
formatter = Formatter (fmt)
-
-
handler.setFormatter( formatter )
-
-
logger.addHandler(handler)
-
-
logger.setLevel(LOG_LEVEL.get(LOG_TYPE.lower()))
-
-
-
# after set every parameters , return logger
-
return logger
// node_id.py
-
from hashlib import sha1 # sha1() method
-
from random import randint # randint method
-
-
import logging
-
-
# here we test our logger
-
from myLogger import InitLogger
-
-
# here we define a method with name of get_node_id
-
# it will return a sha1 data with length = 20bytes = 8*20 = 160 bits
-
# it's namespace length = 20bytes = 160 bits ,which means we could
-
# get a unique node id from 2^160 ids
-
-
def get_node_id():
-
hash = sha1()
-
# print hash just for test
-
# print ( hash)
-
s = ""
-
-
# we try to get a string with length of 20 bytes
-
# and each byte in string is a random integer varied from 0 to 255
-
for i in range(20):
-
s += chr(randint(0,255))
-
-
hash.update(s) # insert s string into hash dict
-
-
# print hash just for test
-
# print ( hash )
-
# print ( s )
-
# here is the test of myLogger.py
-
-
log_file_name = 'node_id.log'
-
-
mylogger = logging.getLogger(log_file_name)
-
mylogger = InitLogger(log_file_name , mylogger)
-
-
mylogger.info('node id :'+hash.digest())
-
-
-
# return hash's binary value
-
return hash.digest ()
-
-
# test method
-
get_node_id()
end
阅读(1421) | 评论(0) | 转发(0) |