This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: logging module: Add `full_module_name` to LogRecord details
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: João Eiras, cool-RR, vinay.sajip
Priority: normal Keywords:

Created on 2016-12-21 16:15 by cool-RR, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg283764 - (view) Author: Ram Rachum (cool-RR) * Date: 2016-12-21 16:15
LogRecord currently supplies `module` that you can show in your messages, but it's just the name of the module without the path. It'll be nice to have `full_module_name` that has the qualified name (e.g. instead of baz.)

Usually it's the logger's name, but not always.
msg322792 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2018-07-31 15:46
If you use the recommended approach of using

logger = logging.getLogger(__name__),

then the logger's name (the name field in the LogRecord) will be the full module path.
msg323307 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2018-08-08 22:16
Closing this as rejected, as when using the recommended approach for naming loggers, you get the full package name which can be output in logs.
msg323315 - (view) Author: Ram Rachum (cool-RR) * Date: 2018-08-09 05:08
Hi Vinay,

Since it's been a couple of years since I opened this ticket, I can't tell you the case in which I wanted to know the full module name of a logger. But it's probably been a logger from a third-party library. Since I can't force a third-party library that doesn't use the `__name__` convention to use it, having an attribute with the full module name would be my only solution to getting that information.
msg323316 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2018-08-09 06:21
The full module name isn't readily available. The value that sets the LogRecord's pathname attribute [which is obtained from the Python code object for the caller] is used to derive the filename attribute (by taking the basename of the pathname) and the filename is used to determine the module attribute (by just dropping the extension from the filename). So if you really need the modulename, you would have to derive it from the pathname.
msg348901 - (view) Author: João Eiras (João Eiras) Date: 2019-08-02 13:55

I ask for this to be reconsidered. The "recommended" approach of using "getLogger(__name__)" comes with some downsides.

In my projects I often have many many files (in one particularly I have hundreds) and creating Logger object for each and every file, so is correct is burdensome, litters the code and forces to add a global variable to the file. So, the easy approach we took was to use using logging.log(...) everywhere.

I've also seen code elsewhere where it is not guaranteed that "getLogger(__name__)" is called with the module __name__, but with some other string.

Or I've seen code where there is a shared Logger() created in some that is then imported into other files.

Overall, relying on is error prone and requires adding more code.

I checked the logging module. The findCaller() function could easily just poke frame.f_globals.get("__name__") to get the module name, and propagate that to the LogRecord.

It's a simple addition. I can make a PR so you can comment further there. The name of the property would be LogRecord.fullModule.

Thank you.
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73222
2019-08-02 13:55:30João Eirassetnosy: + João Eiras
messages: + msg348901
2018-08-09 06:21:13vinay.sajipsetmessages: + msg323316
2018-08-09 05:08:33cool-RRsetmessages: + msg323315
2018-08-08 22:16:03vinay.sajipsetstatus: open -> closed
resolution: rejected
messages: + msg323307

stage: resolved
2018-07-31 15:46:37vinay.sajipsetmessages: + msg322792
2018-07-26 08:06:59berker.peksagsetnosy: + vinay.sajip
2016-12-21 16:15:12cool-RRcreate