Memoru

System Engineering and Programming and IT

python組み込み変数

組み込み変数

variable content note
__file__ 実行script full path
__name__ 実行script module

sample

  • logging_sample.py
#! /usr/bin/env python
"""sample code for __file__
"""
import logging
import logging.config
import os
import sys

### ----------------------------------------- ###
def process():
    logger = logging.getLogger(__name__)
    logger.debug("debug")
    logger.info(os.path.abspath(__file__))
    logger.info(os.path.dirname(__file__))
    logger.info(os.path.basename(__file__))
    logger.info(os.path.split(sys.argv[0])[0])
    logger.info(os.path.split(sys.argv[0])[-1])
    

if __name__ == '__main__':
    logging.config.fileConfig("logging.conf")
    process()

  • log
#============================================================
#DateTime : Friday, June 8, 2018 6:31:42 AM
#CmdLine  : python G:\workplace\py\sandbox\logging_sample.py
#============================================================
[2018/06/08 06:31:42][DEBUG](logging_sample.py:12) debug
[2018/06/08 06:31:42][INFO](logging_sample.py:13) G:\workplace\py\sandbox\logging_sample.py
[2018/06/08 06:31:42][INFO](logging_sample.py:14) G:\workplace\py\sandbox
[2018/06/08 06:31:42][INFO](logging_sample.py:15) logging_sample.py
[2018/06/08 06:31:42][INFO](logging_sample.py:16) G:\workplace\py\sandbox
[2018/06/08 06:31:42][INFO](logging_sample.py:17) logging_sample.py

終了コード: 0
  • logging_debug.conf
[loggers]
keys=root,functions

[handlers]
keys=fileHandler,stderrHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=fileHandler,stderrHandler

[logger_functions]
level=INFO
handlers=fileHandler,stderrHandler
qualname=functions
propagate=0

[handler_fileHandler]
class=handlers.TimedRotatingFileHandler
formatter=simpleFormatter
args=('apps.log','D')

[handler_stderrHandler]
class=StreamHandler
formatter=simpleFormatter
args=(sys.stderr,)

[formatter_simpleFormatter]
format=[%(asctime)s][%(levelname)s](%(filename)s:%(lineno)s) %(message)s
datefmt=%Y/%m/%d %H:%M:%S

reference

tomosoft.jp

// --- end of blog