python —— 使用logging模块简单实现日志系统
来源:互联网 发布:131458淘宝信用查询 编辑:程序博客网 时间:2024/04/30 05:13
使用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, sysimport loggingdef get_root(root): path = os.getcwd() while root != os.path.basename(path): path = os.path.dirname(path) return path #获取系统顶层目录路径,这里为:log_systemROOT = get_root('log_system')#日志存放路径LOG_PATH = os.path.join(ROOT, 'log')#日志对象LOG = None#设置日志级别:NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICALLOG_LEVEL = logging.DEBUG
log.py:
#!/usr/bin/python# -*- coding:gb2312 -*-import osimport timeimport loggingfrom public import configdef 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 osfrom moda import modafrom modb import modbfrom modc import modcfrom public import configfrom public import logdef 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, syssys.path.append('..')from public import configfrom public import logdef 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.pylinux:/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.pyc5 directories, 21 fileslinux:/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.pylinux:/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.pyc5 directories, 22 fileslinux:/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 #
0 0
- python —— 使用logging模块简单实现日志系统
- Python模块:日志输出—logging模块
- Python日志输出——logging模块
- python模块——logging(日志管理)
- python模块——logging(日志管理)
- python模块——logging(日志管理)
- Python日志输出——logging模块
- Python日志输出——logging模块
- python模块——logging(日志管理)
- Python日志输出——logging模块
- python模块——logging(日志管理)
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- ......简单的构造函数
- 远离自尊心
- 第7周作业1-循环大战
- android介绍及四大组件说明。。
- 谁能辅佐天子
- python —— 使用logging模块简单实现日志系统
- 接站
- 2014年4月19日
- 设计模式之原型模式(Prototype)摘录
- 编程中宏的使用
- Unity3D游戏开发之在Unity3D中视频播放功能的实现
- android中的序列化
- 多维度的架构分解
- 读书笔记——《拥有,其实是另一种失去》