python_note
  • Introduction
  • My Python
    • Anaconda
    • argparse
    • datetime
    • json
    • logging
    • numpy
    • open
    • openCC
    • pandas & csv
    • Socket & SocketServer
    • re
    • yaml
    • smtp
    • 物件操作
    • unittest
    • 線程
    • prettytable
    • IO
    • pycurl
    • sys
    • pickle
    • auto-python-to-exe
    • cython
    • nuitka
  • Crawler
    • Urllib & Requests
      • User-agent
      • Percent-Encoding
      • mail code
    • Selenium
    • TCP & UDP
    • 控制字符(control character)
  • Web Development
    • Flask
      • RESTful api
      • Template
      • blueprint
    • Django
      • 環境佈署(windows)
    • 檢查Port
    • Apache
    • 使用者行為
    • jQuery
    • 壓力測試
    • DataTable
    • Bootstrap
    • CSS
    • JavaScript
    • Chart.js
  • Deep Learning
    • Keras 設定
    • RNN
    • LSTM
  • Test
    • T-Test
  • 資料結構
    • Hash
    • 時間複雜度
  • NLP
    • N-gram
    • CKIP
    • 中文轉數字
    • CRF
    • Mutual Information
    • 模糊比對
  • Linebot
    • Heroku
    • 圖文選單
    • channel
  • Linux
    • 常用指令
    • shell script
    • sshfs
    • ssh
    • nodejs & npm
    • debug
  • GCP
    • app engine
    • ssh(gcp)
    • gsutil
    • brabrabra
    • Load Balancer
    • k8s
  • Database
    • mysql
    • elasticsearch
      • Query
      • Backup and Restore
      • elasticdump
      • es2csv
      • ELK
    • mongodb
      • install
      • authentication
      • pymongo
    • sql server
  • go
    • Swarm
  • Docker
    • Kitematic
    • Dockerfile
    • Swarm
  • Git
  • 其他
    • USB軟體保護
    • Windows效能監視器
  • Blockchain
Powered by GitBook
On this page

Was this helpful?

  1. My Python

logging

Log層級(輕至重):debug --> info --> warning --> error --> critical

import logging

# 輸出log檔案
log_file = 'log_file.log'
level = logging.INFO
logging.basicConfig(level=level,
                        format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S',
                        handlers=[logging.FileHandler(log_file, 'a', 'utf-8')])


# 在cmd中顯示
console = logging.StreamHandler()
console.setLevel(level)
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')  # 設定輸出格式
console.setFormatter(formatter)  # handler 設定輸出格式
logging.getLogger('').addHandler(console)  # 加入 handler 到 root logger


# 設定新的name,否則全部都會是root
logger_ = logging.getLogger(os.path.basename(__file__).replace('.py', ''))  # 將檔案名稱設定為name

multiprocessing-logging

如果在multiprocessing時使用logging,需在全域位置宣告logger

或者用queue來存放訊息,然後統一輸出

PreviousjsonNextnumpy

Last updated 5 years ago

Was this helpful?

參考資料:

http://zwindr.blogspot.com/2016/08/python-logging.html
https://hant.helplib.com/GitHub/article_124489
http://hk.voidcc.com/question/p-voslmrpl-ru.html
https://stackoverflow.com/questions/11515944/how-to-use-multiprocessing-queue-in-python
https://morvanzhou.github.io/tutorials/python-basic/multiprocessing/3-queue/