- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用多个处理程序,例如
rootLogger = logging.getLogger()
rootLogger.basicConfig(filename = logfile, level=logging.INFO)
consLogger = logging.StreamHandler()
consLogger.setFormatter(logging.Formatter('%(asctime)-8s %(levelname)-8s %(message)s', '%H:%M:%S'))
consLogger.setLevel(logging.DEBUG)
rootLogger.addHandler(consLogger)
并希望 consLogger(只是 consLogger,而不是所有日志处理程序)被正确包装和缩进,例如
11:03:46 DEBUG text
11:03:47 DEBUG text again
11:03:47 DEBUG some text that is longer than current cons
ole width or at least some pre-defined lin
e length
我应该从哪里开始?
最佳答案
需要注意的是,这确实对您要保存的任何日志都不利(因为它会破坏 grep
或类似的逐行搜索),您可以通过简单的方法做到这一点与 textwrap
模块和自定义 logging.Formatter
子类一起使用。
textwrap
是一个标准库模块,可以对段落进行换行,还可以应用缩进和一些其他格式。真正需要的是通过创建一个自定义格式化程序类来连接它,该类覆盖 format()
方法(它处理将整个日志消息放在一起,而不是处理部分的其他一些方法):
import logging
import textwrap
class WrappedFixedIndentingLog(logging.Formatter):
def __init__(self, fmt=None, datefmt=None, style='%', width=70, indent=4):
super().__init__(fmt=fmt, datefmt=datefmt, style=style)
self.wrapper = textwrap.TextWrapper(width=width, subsequent_indent=' '*indent)
def format(self, record):
return self.wrapper.fill(super().format(record))
# standard stuff
rootLogger = logging.getLogger()
rootLogger.setLevel(logging.DEBUG)
consLogger = logging.StreamHandler()
rootLogger.addHandler(consLogger)
# indent=18 matches fixed width of asctime + levelname + spaces
consLogger.setFormatter(WrappedFixedIndentingLog(
'%(asctime)-8s %(levelname)-8s %(message)s', '%H:%M:%S', indent=18))
message = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
rootLogger.log(logging.DEBUG, message)
输出:
18:48:56 DEBUG Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
关于python - python logging.StreamHandler() 的包装输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23714981/
如何将日志重定向到 TextArea?我试过这样获取日志信息: @Override public void initialize(URL arg0, ResourceBundle arg1) {
我有一个 CLI 脚本,它将所有进程记录到一个日志文件中。CLI 的功能之一是通过以下方式上传大文件将其分成几部分并并行上传。在 linux 中,整个过程就像一个魅力,但在 windows 我似乎无法
我有一个非常简单的结构。但我的两个日志处理程序中只有一个正在从我的模块进行日志记录: 程序.py,支持模块1.py,support_module2.py #program.py import supp
我试图在 python 代码执行的运行时删除 StreamHandler。 if (False == consoleOutput):
我正在编写一个 GeoFire 插件,我试图让 Dart 代码中的查询监听器可用,但没有成功。我似乎无法弄清楚如何为 EventChannel 设置 StreamHandler。要了解我的挣扎,您需要
我不知道如何将信息级消息记录到标准输出,但其他所有信息都记录到标准错误。我已经读过这篇 http://docs.python.org/library/logging.html .有什么建议吗? 最佳答
我想使用 python 的 StreamHandler 日志处理程序。我试过的是, import logging import sys mylogger = logging.getLogger("my
我将我的 Python 代码从 Win10 主机迁移到 WS2012R2。令人惊讶的是,它停止正常运行,现在显示警告消息:“UnicodeEncodeError:‘charmap’编解码器无法对位置
我使用多个处理程序,例如 rootLogger = logging.getLogger() rootLogger.basicConfig(filename = logfile, level=loggi
我一直在寻找一个很好的例子来将进程输出和错误流写入日志文件。我使用 apache-commons exec 库来执行我的进程。按照代码示例进行演示 public static int executeC
我有如下设置日志记录: def setUp(): LOG_FORMAT = '%(asctime)s %(levelname)-8s %(name)s %(message)s' #LO
我正在尝试为我的日志文件和流设置不同的记录器级别,并且我(似乎)遵循了演示(https://docs.python.org/3/howto/logging-cookbook.html)到点。但是,在我
重定向正常日志记录的输出工作正常: import contextlib import io import logging std_out_capture = io.StringIO() with co
使用单个 StreamHandler 是否安全?在多处理环境中? 更准确地说,只有一个 StreamHandler 可以简单地将所有进程的日志记录打印到 stdout 吗?像这样,例如: import
我不知道为什么它无法记录该消息,我认为一切都已正确设置。 logging.DEBUG 定义在 logging 模块下 import logging import sys logger = loggin
假设我有如下脚本,我无法编辑它: import logging logger = logging.getLogger('simple_example') logger.setLevel(logging
我有一个普通的 python(非 Django)项目,我正在尝试 tie Raven into the logging setup . 在我们当前的设置下,我们使用一个简单的日志配置: import
我的 Python 应用程序中有一个 RotatingFileHandler 和 StreamHandler 。这是在 docker 容器中的 apache 服务器中运行的。因此,我将 apache
我在基本配置中设置了两个日志处理程序。 FileHandler 记录到文件,StreamHandler 记录到标准输出。 logging.basicConfig( format="%(asct
python 模块 paramiko 抛出一个奇怪的异常,如磁贴上所示。最后我找到了它真正出现的位置,它在模块 logging 中。当我使用 pydev 在 Eclipse 中的行上设置断点,并将行放
我是一名优秀的程序员,十分优秀!