LoginSignup
0
0

More than 1 year has passed since last update.

Python - ログ

Posted at

configparser

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {
    "debug": True
}
config["web_server"] = {
    "host": "127.0.0.1",
    "port": 80
}
config["db_server"] = {
    "host": "127.0.0.1",
    "port": 3306
}

with open("config.ini", "w") as config_file:
    config.write(config_file)
import configparser

config = configparser.ConfigParser()
config.read("config.ini")
config["web_server"]["host"]

yaml

import yaml

with open("config,yml", "w") as yaml_file:
    yaml.dump({
        "web_server": {
            "host": "127.0.0.1",
            "port": 80
        },
        "db_server": {
            "host": "127.0.0.1",
            "port": 3306
        }
    }, yaml_file, default_flow_style=False)
import yaml

with open("config,yml", "r") as yaml_file:
    data = yaml.load(yaml_file, Loader=yaml.Loader)
    data["web_server"]["host"]

logging

ロギング

import logging

# INFO 以上のログを出力(デフォルトでは、WARNING 以上のログを出力する。)
logging.basicConfig(filename="test.log", level=logging.INFO)

logging.critical("critical")
logging.error("error")
logging.warning("warning")
logging.info("info {}".format("test"))
logging.debug("debug")

フォーマッタ

import logging

# フォーマッタでメッセージをカスタマイズする。
formatter = '%(asctime)s:%(levelname)s'
logging.basicConfig(level=logging.INFO, format=formatter)

logging.critical("critical")
logging.error("error")
logging.warning("warning")
logging.info("info")
logging.debug("debug")

ロガー

import logging

logging.basicConfig(level=logging.INFO)

logging.info("info")

# ログレベルを、DEBUG に変える。
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.debug("debug")

ハンドラー

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# logger をファイルに書き込む
h = logging.FileHandler("test.log")
logger.addHandler(h)

logging.info("from logging info")
logger.info("from logger info")

フィルタ

import logging

logging.basicConfig(level=logging.INFO)

# pass が含まれている場合は、ログに出力しない。
class NoPassFilter(logging.Filter):
    def filter(self, record):
        log_message = record.getMessage()
        return 'pass' not in log_message


logger = logging.getLogger(__name__)
logger.addFilter(NoPassFilter())
logger.info('from main')
logger.info('from main pass = "test"')

ロギング コンフィグ

import logging.config


# ロギング コンフィグから読み込む。
logging.config.fileConfig('logging.ini')
logger = logging.getLogger(__name__)

logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')
logging.ini
[loggers]
keys=root,simpleExample

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=WARNING
handlers=consoleHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

email

virtualenv

# virtualenv をインストール
pip install virtualenv

# virtualenv を作成
virtualenv my_python_env

# 作成した virtualenv を利用。
source my_python_env/bin/activate

# 利用している環境を確認
which python
/Users/XXX/PycharmProjects/my_python_env/bin/python

# 元の環境に戻る
deactivate
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0