-3

I am lazy and want to avoid this line in every python file which uses logging:

logger = logging.getLogger(__name__)

In january I asked how this could be done, and found an answer: Avoid `logger=logging.getLogger(__name__)`

Unfortunately the answer there has the drawback, that you loose the ability to filter.

I really want to avoid this useless and redundant line.

Example:

import logging

def my_method(foo):
    logging.info() 

Unfortunately I think it is impossible do logger = logging.getLogger(__name__) implicitly if logging.info() gets called for the first time in this file.

Is there anybody out there who knows how to do impossible stuff?

Update

I like Don't Repeat Yourself. If most files contain the same line at the top, I think this is a repetition. It looks like WET. The python interpreter in my head needs to skip this line every time I look there. My subjective feeling: this line is useless bloat. The line should be the implicit default.

Community
  • 1
  • 1
guettli
  • 25,042
  • 81
  • 346
  • 663
  • Is it really *that hard* to define that *one line* at the top of your module? You already had to type out `import logging`. – Martijn Pieters Aug 15 '16 at 09:39
  • @MartijnPieters I don't type "import logging". I have an IDE which does this. I type "logging.[MAGIC-KEY-STROKE]" in the method and the IDE inserts the import statement at the top. Yes, I don't want to type this line. It's bloat. I want less code, not more. – guettli Aug 15 '16 at 09:46
  • 2
    It is not bloat; it is an explicit declaration that you want a logger object with that name. In a package you could reuse that logger for all submodules if you don't care about the name. You can easily use different hierarchy of names. Moreover, anything else would require a custom module subclass with a custom `__getattr__` that then checks the call frame for a `__name__` key in the globals to produce the same object. Python prefers being explicit over such magic. – Martijn Pieters Aug 15 '16 at 09:52
  • 1
    If you are using an IDE, then perhaps it could be something the IDE does for you. Perhaps it has auto-expanding snippet support? You type `logger` and the import and the `getLogger(__name__)` call are inserted for you? This is not something Python has to solve for you. – Martijn Pieters Aug 15 '16 at 09:54
  • @MartijnPieters you said "It is not bloat". I updated the question. See "Update". But I think this paragraph I added did not add anything substantial. – guettli Aug 16 '16 at 05:13

2 Answers2

2

Think well if you really want to do this.

Create a Module e.g. magiclog.py like this:

import logging
import inspect

def L():
    # FIXME: catch indexing errors
    callerframe = inspect.stack()[1][0]
    name = callerframe.f_globals["__name__"]
    # avoid cyclic ref, see https://docs.python.org/2/library/inspect.html#the-interpreter-stack
    del callerframe
    return logging.getLogger(name)

Then you can do:

from magiclog import L
L().info("it works!")
Torben Klein
  • 2,943
  • 1
  • 19
  • 24
  • Cool, this looks like the first step. Now this should be executed if I do `import logging` and `logging.info("it works!")` – guettli Aug 16 '16 at 05:52
  • 1
    Well you can of course monkeypatch the functions in the logging module, but then you're really crossing the line into "evil" territory. (i.e. anybody inheriting your code will have a fun time). – Torben Klein Aug 16 '16 at 06:05
1

I am lazy and want to avoid this line in every python file which uses logging:

logger = logging.getLogger(__name__)

Well, it's the recommended way:

A good convention to use when naming loggers is to use a module-level logger, in each module which uses logging, named as follows:

logger = logging.getLogger(__name__)

This means that logger names track the package/module hierarchy, and it’s intuitively obvious where events are logged just from the logger name.

That's a quote from the official howto.

I like Don't Repeat Yourself. If most files contain the same line at the top, I think this is a repetition. It looks like WET. The python interpreter in my head needs to skip this line every time I look there. My subjective feeling: this line is useless bloat. The line should be the implicit default.

It follows "Explicit is better than implicit". Anyway you can easily change a python template in many IDEs to always include this line or make a new template file.

Stan Prokop
  • 5,579
  • 3
  • 25
  • 29
  • 1
    And now to something completely different, but don't take it too serious: Ironing socks was the recommend way several years ago. Every good housewife did it. Most people stopped doing this none sense. – guettli Aug 22 '16 at 09:53
  • 1
    I got 99 programming problems but explicit one-liner for logging setup ain't one. That's my nonserious reply. – Stan Prokop Aug 22 '16 at 19:13