abseil / abseil-py

Abseil Common Libraries (Python)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

log get file line number by default

lucasjinreal opened this issue · comments

Currrent either a way to using format to log or prints any usable information such as line number..

The log format is fixed (and includes the file / line number) unless you override the formatter. Are you asking how to change the format so it doesn't include the line numbers?

Examples of logs:

I1231 23:59:59.000000 12345 logging_functional_test_helper.py:65] This line is VLOG level 0
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:65] This line is log level 0
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:70] Interesting Stuff\0
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:71] Interesting Stuff with Arguments: 42
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:73] Interesting Stuff with Dictionary
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:123] This should appear 5 times.
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:123] This should appear 5 times.
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:123] This should appear 5 times.
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:123] This should appear 5 times.
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:123] This should appear 5 times.
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:76] Info first 1 of 2
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:77] Info 1 (every 3)
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:76] Info first 2 of 2
I1231 23:59:59.000000 12345 logging_functional_test_helper.py:77] Info 4 (every 3)

Simply python script does not print anything, why?:

[10:44:27] fagjin:~ $ python a.py 
[10:44:29] fagjin:~ $ cat a.py  
from absl import logging as logger

a = 3434
logger.info('{}'.format(a))

I am really confusing right now:

from absl import logging as logger
import logging


fmt = '[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s'
formatter = logging.Formatter(fmt)

logger.get_absl_handler().setFormatter(formatter)
logger.set_verbosity('debug')

a = 3434
logger.info('{}'.format(a))

Doesn't applied format...........

Re #124 (comment):

The verbosity of logs before flags are parsed (via absl.app.run(main)) is WARNING, so only WARNING/ERROR/CRITICAL logs are logged. After absl.app.run(main), the verbosity is INFO. Example:

$ cat t.py
from absl import app
from absl import logging
# Logging before flags are parsed (or app.run is called)
logging.info("info before")
logging.warning("warning before")
def main(argv):
    del argv  # Unused.
    # Logging after flags are parsed from app.run(main)
    logging.info("info after")
    logging.warning("warning after")
if __name__ == "__main__":
    app.run(main)
$ python t.py
WARNING:absl:warning before
I1202 20:37:36.482917 4517098944 t.py:13] info after
W1202 20:37:36.483098 4517098944 t.py:14] warning after

Re #124 (comment):

Again, this must be done inside the main function, otherwise the format always defaults to the standard logging module:

$ cat t.py
import logging
from absl import app
from absl import logging as logger
def main(argv):
    del argv  # Unused.
    fmt = "[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s"
    formatter = logging.Formatter(fmt)
    logger.get_absl_handler().setFormatter(formatter)
    logger.set_verbosity("debug")
    a = 3434
    logger.info("{}".format(a))
if __name__ == "__main__":
    app.run(main)
$ python t.py
[INFO 2019-12-02 20:40:29,394 t.py:15] 3434