Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 禁用某些邮件的格式_Python_Logging_Sh - Fatal编程技术网

Python 禁用某些邮件的格式

Python 禁用某些邮件的格式,python,logging,sh,Python,Logging,Sh,我正在与pythons日志库一起开发一个数据分析管道。我正在记录关于正在运行哪些步骤的自定义消息,以及所调用程序的原始输出。我正在使用sh.py简化shell命令的调用。它允许我定义一个回调函数来处理STDOUT/STDERR的每一行 我希望能够定义回调函数,将每一行记录到日志文件中,而不进行格式化(这些程序输出很多行): 生成的日志文件如下所示: """<br> 2016-01-22|{INFO}: Starting $SOME_PROGRAM$ on $SOME_FILE$<

我正在与pythons日志库一起开发一个数据分析管道。我正在记录关于正在运行哪些步骤的自定义消息,以及所调用程序的原始输出。我正在使用
sh.py
简化shell命令的调用。它允许我定义一个回调函数来处理STDOUT/STDERR的每一行

我希望能够定义回调函数,将每一行记录到日志文件中,而不进行格式化(这些程序输出很多行):

生成的日志文件如下所示:

"""<br>
2016-01-22|{INFO}: Starting $SOME_PROGRAM$ on $SOME_FILE$<br>
program_output_line<br>
program_output_line<br>
program_output_line<br>
program_output_line<br>
program_output_line<br>
2016-01-22|{INFO}: Finished running $SOME_PROGRAM$<br>
"""
“”“
2016-01-22 |{INFO}:在$SOME_文件$
程序输出线
程序输出线
程序输出线
程序输出线
程序输出线
2016-01-22 |{INFO}:已完成运行$SOME_程序$
"""

有人知道我是如何做到这一点的吗?

您可以为
记录器使用的处理程序设置
logging.Formatter
对象。它应该实现
格式(记录)
方法,该方法将返回实际显示的字符串

import sh
import logging

class MyFormatter(logging.Formatter):
    def __init__(self, *args, **kwargs):
        logging.Formatter.__init__(self, *args, **kwargs)
        self._enableFormatFlag = True

    def enableFormat(self):
        self._enableFormatFlag = True
    def disableFormat(self):
        self._enableFormatFlag = False

    def format(self, record):
        if self._enableFormatFlag:
            return logging.Formatter.format(self, record)
        else:
            return record.getMessage()

logger = logging.getLogger('myapp')
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
logger.addHandler(handler)
formatter = MyFormatter(fmt="prefix[%(message)s]suffix")
handler.setFormatter(formatter)


def callback(line):
    logger.info(line.strip())

logger.info("Will start program")
formatter.disableFormat()
sh.ls(['-l'],_out=callback)
formatter.enableFormat()
logger.info("Finished program")

PS:我之前的回答是关于获取回调的,对不起。

您可以查看logging.config


基本上,您可以定义一个专用的记录器,该记录器写入与根记录器相同的文件,但使用精简的格式化程序,如
format=%(message)s
。在call\u back中,您将显式地获取该记录器:
logging.getlogger(logger\u name)
您可以通过扩展
logging.Formatter
类来实现这一点。如果日志消息附带可选元数据
simple=True
-,则不会为该特定消息提供格式设置

class ConditionalFormatter(logging.Formatter):
    def format(self, record):
        if hasattr(record, 'simple') and record.simple:
            return record.getMessage()
        else:
            return logging.Formatter.format(self, record)


rotating_file_formatter = ConditionalFormatter('%(asctime)s %(levelname)s - %(message)s')
rotating_file.setFormatter(rotating_file_formatter)
# ...
  • 带格式设置

    logging.info("starting application ...")
    
    logging.info("starting application ...", extra={'simple': True})
    
    日志输出:

    2020-05-01 19:38:05,337 INFO - starting application ...
    
    starting application ...
    
  • 不带格式

    logging.info("starting application ...")
    
    logging.info("starting application ...", extra={'simple': True})
    
    日志输出:

    2020-05-01 19:38:05,337 INFO - starting application ...
    
    starting application ...
    

完整示例 在这个脚本中,我使用控制台日志和文件日志。在常规日志中,我调用了一个命令
ls-ahl/
,并将其所有输出直接传送到日志,而无需格式化:

#!/usr/bin/env python3

import logging
import subprocess
import sys
from logging.handlers import RotatingFileHandler


def initialize_logger(log_file_path):
    rotating_file = RotatingFileHandler(log_file_path, mode='a', maxBytes=5 * 1024 * 1024, backupCount=5, encoding=None, delay=0)

    class ConditionalFormatter(logging.Formatter):
        def format(self, record):
            if hasattr(record, 'simple') and record.simple:
                return record.getMessage()
            else:
                return logging.Formatter.format(self, record)

    rotating_file_formatter = ConditionalFormatter('%(asctime)s %(levelname)s - %(message)s')
    rotating_file.setFormatter(rotating_file_formatter)
    rotating_file.setLevel(logging.DEBUG)

    console_logger = logging.StreamHandler(sys.stdout)
    console_logger_formatter = logging.Formatter('%(message)s')
    console_logger.setFormatter(console_logger_formatter)
    console_logger.setLevel(logging.INFO)

    root_logger = logging.getLogger()
    root_logger.setLevel(logging.DEBUG)
    root_logger.addHandler(rotating_file)
    root_logger.addHandler(console_logger)


def main():
    initialize_logger('/var/log/my-app.log')
    logging.info('starting application ...')

    command = ["ls", "-ahl"]
    working_directory = '/'
    logging.info(f'running {command} ...')
    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=working_directory)

    with process.stdout:
        for line in iter(process.stdout.readline, b''):
            line = line.decode('utf-8')
            line = line.strip()
            logging.debug(line, extra={'simple': True})

    exit_code = process.wait()
    if exit_code == 0:
        logging.debug(f'command "{command}" finished successfully')
    else:
        logging.debug(f'command "{command}" failed with exit code {exit_code}')

    logging.info('stopping application')


if __name__ == '__main__':
    main()
标准:

starting application ...
running ['ls', '-ahl'] ...
stopping application
输出文件
/var/log/my app.log

2020-05-01 19:48:06,347 INFO - starting application ...
2020-05-01 19:48:06,347 INFO - running ['ls', '-ahl', '/'] ...
total 112K
drwxr-xr-x  25 root root 4.0K Apr 30 06:55 .
drwxr-xr-x  25 root root 4.0K Apr 30 06:55 ..
drwxr-xr-x   2 root root 4.0K Mar 31 06:39 bin
drwxr-xr-x   3 root root 4.0K May  1 06:02 boot
drwxrwxr-x   2 root root 4.0K Oct 11  2018 cdrom
drwxr-xr-x  20 root root 4.2K Apr 30 06:54 dev
drwxr-xr-x 154 root root  12K Apr 30 06:54 etc
drwxr-xr-x   3 root root 4.0K Oct 11  2018 home
lrwxrwxrwx   1 root root   33 Apr 30 06:55 initrd.img -> boot/initrd.img-4.15.0-99-generic
lrwxrwxrwx   1 root root   33 Apr 30 06:55 initrd.img.old -> boot/initrd.img-4.15.0-96-generic
drwxr-xr-x  21 root root 4.0K Oct 20  2018 lib
drwxr-xr-x   2 root root 4.0K Oct 20  2018 lib32
drwxr-xr-x   2 root root 4.0K Jul 25  2018 lib64
drwx------   2 root root  16K Oct 11  2018 lost+found
drwxr-xr-x   4 root root 4.0K Oct 15  2018 media
drwxr-xr-x   3 root root 4.0K Jun 20  2019 mnt
drwxrwxrwx  23 root root 4.0K May  1 16:56 opt
dr-xr-xr-x 302 root root    0 Apr 23 19:57 proc
drwx------   6 root root 4.0K Apr 29 22:31 root
drwxr-xr-x  40 root root 1.4K May  1 14:44 run
drwxr-xr-x   2 root root  12K Feb  9 12:00 sbin
drwxr-xr-x  15 root root 4.0K Apr 25 11:15 snap
drwxr-xr-x   2 root root 4.0K Jul 25  2018 srv
dr-xr-xr-x  13 root root    0 May  1 18:53 sys
drwxrwxrwt  32 root root 4.0K May  1 19:39 tmp
drwxr-xr-x  11 root root 4.0K Oct 20  2018 usr
drwxr-xr-x  15 root root 4.0K Jun 20  2019 var
lrwxrwxrwx   1 root root   30 Apr 30 06:55 vmlinuz -> boot/vmlinuz-4.15.0-99-generic
lrwxrwxrwx   1 root root   30 Apr 30 06:55 vmlinuz.old -> boot/vmlinuz-4.15.0-96-generic
2020-05-01 19:48:06,357 DEBUG - command "['ls', '-ahl', '/']" finished successfully
2020-05-01 19:48:06,357 INFO - stopping application