一、logging直接寫在代碼中
-
-
-
-
-
-
- def log_test01():
- import logging
- import logging.handlers
- LOG_FILE = "../log/test01.log"
- handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 20*1024*1024, backupCount = 10);
- fmt = "%(asctime)s - %(name)s - %(levelname)s - %(message)s - [%(filename)s:%(lineno)s]"
- formatter = logging.Formatter(fmt);
- handler.setFormatter(formatter);
-
- logger = logging.getLogger('xzs');
- logger.addHandler(handler);
- logger.setLevel(logging.DEBUG)
-
- logger.debug("Hello boy, Debug");
- logger.info("Hello boy, Info");
-
- if __name__ == "__main__":
- log_test01()
-
輸出結(jié)果
- 2012-06-04 14:48:13,606 - xzs - DEBUG - Hello boy, Debug - [xlog.py:20]
- 2012-06-04 14:48:13,606 - xzs - INFO - Hello boy, Info - [xlog.py:21]
這種直接配置比較簡單,但不夠靈活
二、通過配置文件來配置輸出>>>http://blog.csdn.net/naiveloafer/article/details/7630903
來自:http://blog.csdn.net/naiveloafer/article/details/7630673