0
考慮以下代碼:Python記錄奇怪的行爲
ubuntu_logger = logging.getLogger('ubuntu-logger')
mail_handler = MailHandler()
mail_handler.setLevel(logging.INFO)
ubuntu_logger.addHandler(mail_handler)
filepath = "/home/ubuntu/logs/central.log"
formatter = logging.Formatter('[%(asctime)s - %(name)s - %(levelname)s]: %(message)s')
central_handler = logging.handlers.RotatingFileHandler(
filename=filepath,
mode="a+"
)
central_handler.setLevel(logging.DEBUG)
central_handler.setFormatter(formatter)
ubuntu_logger.addHandler(central_handler)
我serverutils.logutils
創建此處理程序,自定義的Python模塊。然後,我將其導入到我的後臺程序服務腳本,這是root
用戶運行:
from serverutils.logutils import ubuntu_logger as logger, DEFAULT_LOGGING_CONFIG
logger.info('pydaemons launching...')
通過上面的代碼中,ubuntu_logger
來說確實什麼都沒有。改變像下面的代碼後,ubuntu_logger
作品如預期,除了根記錄:
import logging
from serverutils.logutils import ubuntu_logger as logger, DEFAULT_LOGGING_CONFIG
config = DEFAULT_LOGGING_CONFIG # Fancy format, log level DEBUG
config.update(filename='/home/ubuntu/test.log')
logging.basicConfig(**config)
logging.error('omg, this works')
logger.info('pydaemons launching...')
我缺少什麼?
我會更新這個問題,看起來我在錯誤的方向看 – Mazyod 2014-10-06 08:43:07
@Mazyod:如果你這樣做,請刪除*編輯*標題;我們都可以看到編輯歷史,無需在問題本身中對其進行評論。 :-) – 2014-10-06 08:44:02
好的,謝謝。沒有標題更新。 – Mazyod 2014-10-06 08:46:31