how to write log into file
akter-sust opened this issue · comments
Hi,
I am trying to write log into file directly instead of showing in command window. I have seen following issues about it
#83
#111
I tried them but I cant see anything in the file.
My code is
import os
from absl import logging
if not os.path.exists('/opt/log/'):
os.makedirs('/opt/log/')
logging.get_absl_handler().use_absl_log_file('absl_logging', '/opt/log/')
logging.info('test')
logging.debug('test debug')
I cant see anything in that file though file was created
Is this your exact code? Note that logs before the absl.app.run(main)
call are logged to stderr (only WARN and more severe levels, others are not logged).
Like #83 (comment), you need to call use_absl_log_file
inside your main
function.
yes, that is my code to test.
I got the answer, log will write to file from the function after calling absl.app.run(<function>)
is there any way to write in file without calling absl.app.run(<function>)
because for server side service, sometimes it is required
The absl.logging
package defines a few command line flags from absl.flags
, so that it has special logic to check whether flags are parsed or not. For example, you can control the log directory via the --log_dir
flag define here
Flag parsing is typically done by calling absl.app.run
, and your server's main could be modified to use it.
But if you do not want command line flags parsing (i.e. always use the default flag values), you can try to call absl.flags.FLAGS.mark_as_parsed()
, after that, logs should go to files.
Let me know if this works for you.
I have tried with following code, but it does not write into files
import os
import absl
from absl import logging
if not os.path.exists('./'):
os.makedirs('./')
logging.get_absl_handler().use_absl_log_file('absl_logging', './')
absl.flags.FLAGS.mark_as_parsed()
logging.info('test')
logging.debug('test debug')
Without using app.run
, the default verbosity is WARNING
, i.e. only WARNING
and more severe logs are logged, that's why your info/debug
logs are not written. If you call logging.warning('warning')
, it should be there.
You can e.g. call logging.set_verbosity(logging.INFO)
to make the verbosity INFO
.
When using app.run
, the default is INFO
. The the reason why without app.run
the default is WARNING
: it matches standard logging's behavior, when you haven't configured logging, only WARNING
and more severe logs are written to stderr.
Hope it helps.
Thank you @yilei
Now it works, my code is
import os
import absl
from absl import logging
if not os.path.exists('./'):
os.makedirs('./')
logging.get_absl_handler().use_absl_log_file('absl_logging', './')
absl.flags.FLAGS.mark_as_parsed()
logging.set_verbosity(logging.INFO)
logging.info('test')
Without using
app.run
, the default verbosity isWARNING
, i.e. onlyWARNING
and more severe logs are logged, that's why yourinfo/debug
logs are not written. If you calllogging.warning('warning')
, it should be there.You can e.g. call
logging.set_verbosity(logging.INFO)
to make the verbosityINFO
.When using
app.run
, the default isINFO
. The the reason why withoutapp.run
the default isWARNING
: it matches standard logging's behavior, when you haven't configured logging, onlyWARNING
and more severe logs are written to stderr.Hope it helps.
Hi @yilei! Is this behaviour referenced anywhere in the abseil documentation? Thanks!
@stefanistrate This is only mentioned in the --verbosity
flag's help string.
Thank you @yilei
Now it works, my code is
import os import absl from absl import logging if not os.path.exists('./'): os.makedirs('./') logging.get_absl_handler().use_absl_log_file('absl_logging', './') absl.flags.FLAGS.mark_as_parsed() logging.set_verbosity(logging.INFO) logging.info('test')
I tried that code, and it did not work. There was only INFO:absl:test
in the command window. I can't see anything in the file.
@eshijia do you have a minimal but complete reproducible example? snippets aren't great since how/when they are executed is important.
I have solved the problem. The key point is that if you do not use app.run()
, you need to add logging.use_absl_handler()
at the beginning of the code.
This code does not work:
import os
import absl
from absl import logging
if not os.path.exists('./'):
os.makedirs('./')
logging.get_absl_handler().use_absl_log_file('absl_logging', './')
absl.flags.FLAGS.mark_as_parsed()
logging.set_verbosity(logging.INFO)
logging.info('test')
But this code works:
import os
import absl
from absl import logging
logging.use_absl_handler()
if not os.path.exists('./'):
os.makedirs('./')
logging.get_absl_handler().use_absl_log_file('absl_logging', './')
absl.flags.FLAGS.mark_as_parsed()
logging.set_verbosity(logging.INFO)
logging.info('test')
I have to say that even that this lib comes from Google guys, is not UX friendly. Guys, please add some examples how to use it! I'm working with ml-metadata and they are using your crappy stuff and I have no idea how to enabling logging. And please, add the example with absl.run()! Only thanks to @eshijia I'm able to see what's up not you, creators.