python —— 使用logging模块简单实现日志系统

使用logging模块写的一个小型日志系统,可以运行在windows和linux系统。设计有些地方不够合理,需要改进。

源代码下载地址:http://download.csdn.net/detail/wangyuling1234567890/7219649


文档组织结构:

linux:/mnt/hgfs/vmware-share/log_system # tree -a ../log_system/
../log_system/
|-- __init__.py
|-- log
|-- main.py
|-- moda
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- moda.py
|   `-- moda.pyc
|-- modb
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- modb.py
|   `-- modb.pyc
|-- modc
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- modc.py
|   `-- modc.pyc
`-- public
    |-- __init__.py
    |-- __init__.pyc
    |-- config.py
    |-- config.pyc
    |-- log.py
    `-- log.pyc

config.py:

#!/usr/bin/python
# -*- coding:gb2312 -*-

import os, sys
import logging

def get_root(root):
    path = os.getcwd()
    
    while root != os.path.basename(path):
        path = os.path.dirname(path)
        
    return path
    
#获取系统顶层目录路径,这里为:log_system
ROOT = get_root('log_system')

#日志存放路径
LOG_PATH = os.path.join(ROOT, 'log')

#日志对象
LOG = None

#设置日志级别:NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL
LOG_LEVEL = logging.DEBUG

log.py:

#!/usr/bin/python
# -*- coding:gb2312 -*-

import os
import time
import logging

from public import config

def init_log(logger_name, log_name):
    cut_time = time.strftime("%Y%m%d-%H%M%S", time.localtime())
    file_name = os.path.join(config.LOG_PATH, log_name + cut_time + '.log')
    
    logger = logging.getLogger(logger_name)
    logger.setLevel(config.LOG_LEVEL)
    
    fh = logging.FileHandler(file_name)
    fh.setLevel(config.LOG_LEVEL)
    
    formatter = logging.Formatter('[%(module)s] %(asctime)s - [%(message)s]')
    fh.setFormatter(formatter)
    
    logger.addHandler(fh)
    
    config.LOG = logger
    return 

main.py:

#!/usr/bin/python
# -*- coding:gb2312 -*-

import os

from moda import moda
from modb import modb
from modc import modc

from public import config
from public import log

def test():
    config.LOG.debug('debug')
    config.LOG.info('info')
    config.LOG.warning('warning')
    config.LOG.error('error')
    config.LOG.critical('critical')

    moda.test()
    modb.test()
    modc.test()
    
if __name__ == '__main__':
    name = os.path.basename(__file__)
    log.init_log(name, name)

    test()

moda.py:

#!/usr/bin/python
# -*- coding:gb2312 -*-

import os, sys
sys.path.append('..')

from public import config
from public import log

def test():
    config.LOG.debug('debug')
    config.LOG.info('info')
    config.LOG.warning('warning')
    config.LOG.error('error')
    config.LOG.critical('critical')

if __name__ == '__main__':
    name = os.path.basename(__file__)
    log.init_log(name, name)
    test()

运行结果:

linux:/mnt/hgfs/vmware-share/log_system # python main.py
linux:/mnt/hgfs/vmware-share/log_system # tree -a ../log_system/
../log_system/
|-- __init__.py
|-- log
|   `-- main.py20140419-193539.log
|-- main.py
|-- moda
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- moda.py
|   `-- moda.pyc
|-- modb
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- modb.py
|   `-- modb.pyc
|-- modc
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- modc.py
|   `-- modc.pyc
`-- public
    |-- __init__.py
    |-- __init__.pyc
    |-- config.py
    |-- config.pyc
    |-- log.py
    `-- log.pyc

5 directories, 21 files
linux:/mnt/hgfs/vmware-share/log_system # cat log/main.py20140419-193539.log
[main] 2014-04-19 19:35:39,745 - [debug]
[main] 2014-04-19 19:35:39,745 - [info]
[main] 2014-04-19 19:35:39,745 - [warning]
[main] 2014-04-19 19:35:39,745 - [error]
[main] 2014-04-19 19:35:39,746 - [critical]
[moda] 2014-04-19 19:35:39,746 - [debug]
[moda] 2014-04-19 19:35:39,746 - [info]
[moda] 2014-04-19 19:35:39,746 - [warning]
[moda] 2014-04-19 19:35:39,746 - [error]
[moda] 2014-04-19 19:35:39,746 - [critical]
[modb] 2014-04-19 19:35:39,746 - [debug]
[modb] 2014-04-19 19:35:39,746 - [info]
[modb] 2014-04-19 19:35:39,746 - [warning]
[modb] 2014-04-19 19:35:39,746 - [error]
[modb] 2014-04-19 19:35:39,746 - [critical]
[modc] 2014-04-19 19:35:39,746 - [debug]
[modc] 2014-04-19 19:35:39,746 - [info]
[modc] 2014-04-19 19:35:39,746 - [warning]
[modc] 2014-04-19 19:35:39,746 - [error]
[modc] 2014-04-19 19:35:39,746 - [critical]
linux:/mnt/hgfs/vmware-share/log_system # cd moda/
linux:/mnt/hgfs/vmware-share/log_system/moda # python moda.py
linux:/mnt/hgfs/vmware-share/log_system/moda # tree ../../log_system/
../../log_system/
|-- __init__.py
|-- log
|   |-- main.py20140419-193539.log
|   `-- moda.py20140419-193622.log
|-- main.py
|-- moda
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- moda.py
|   `-- moda.pyc
|-- modb
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- modb.py
|   `-- modb.pyc
|-- modc
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- modc.py
|   `-- modc.pyc
`-- public
    |-- __init__.py
    |-- __init__.pyc
    |-- config.py
    |-- config.pyc
    |-- log.py
    `-- log.pyc

5 directories, 22 files
linux:/mnt/hgfs/vmware-share/log_system/moda # cat ../log/moda.py20140419-193622.log
[moda] 2014-04-19 19:36:22,015 - [debug]
[moda] 2014-04-19 19:36:22,015 - [info]
[moda] 2014-04-19 19:36:22,015 - [warning]
[moda] 2014-04-19 19:36:22,015 - [error]
[moda] 2014-04-19 19:36:22,015 - [critical]
linux:/mnt/hgfs/vmware-share/log_system/moda #


你可能感兴趣的:(python)