Skip to content

Issue: logging.basicConfig(level=logging.ERROR) unexpectedly changes log format of custom logger #151121

@E5C8F

Description

@E5C8F

Bug report

Bug description:

Issue: logging.basicConfig(level=logging.ERROR) unexpectedly changes log format of custom logger

Environment

  • Python version: 3.11
  • OS: Linux / Alibaba Cloud WebIDE (also reproducible locally)

Steps to Reproduce
Run the following code:

import logging
loger = logging.getLogger(__name__)
loger.error('start1')
logging.basicConfig(level=logging.ERROR)
loger.error('start1')

Actual Output

start1
ERROR:__main__:start1

Expected Output
According to Python logging documentation, basicConfig should not change the output format when only level is specified (no format parameter). The format of both log lines should be identical (e.g., both ERROR:__main__:start1).

Description

  • The first loger.error('start1') prints only the message start1, missing log level and logger name.
  • The second call (after basicConfig(level=logging.ERROR)) prints the normal format ERROR:__main__:start1.
  • basicConfig only set the level parameter without specifying format, yet it altered the output format for the custom logger.

Additional Notes

  • The issue is reproducible both in Alibaba Cloud WebIDE and local environment.
  • Direct calls like logging.error('start2') always print the normal format and are unaffected.

问题报告:logging.basicConfig(level=logging.ERROR) 意外改变自定义 logger 的日志格式

环境

  • Python 版本:3.11
  • 操作系统:Linux / 阿里云 WebIDE(本地同样复现)

重现步骤
运行以下代码:

import logging
loger = logging.getLogger(__name__)
loger.error('start1')
logging.basicConfig(level=logging.ERROR)
loger.error('start1')

实际输出

start1
ERROR:__main__:start1

期望输出
根据 Python logging 文档,当只指定 level 参数(未指定 format)时,basicConfig 不应改变输出格式。期望两次输出的格式一致(例如均为 ERROR:__main__:start1)。

问题描述

  • 第一次 loger.error('start1') 仅输出消息内容 start1,缺少日志级别和 logger 名称。
  • 第二次调用(在 basicConfig(level=logging.ERROR) 之后)输出正常格式 ERROR:__main__:start1
  • basicConfig 仅设置了 level 参数,未设置 format,但却改变了自定义 logger 的输出格式。

补充说明

  • 该现象在阿里云 WebIDE 和本地环境均可稳定复现。
  • 直接调用 logging.error('start2') 始终输出正常格式,不受上述影响。

CPython versions tested on:

3.11, 3.13

Operating systems tested on:

Linux, Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions