Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to write log into file #117

Closed
akter-sust opened this issue Oct 23, 2019 · 12 comments
Closed

how to write log into file #117

akter-sust opened this issue Oct 23, 2019 · 12 comments

Comments

@akter-sust
Copy link

akter-sust commented Oct 23, 2019

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

@yilei
Copy link
Contributor

yilei commented Oct 24, 2019

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.

@akter-sust
Copy link
Author

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

@yilei
Copy link
Contributor

yilei commented Oct 25, 2019

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.

@akter-sust
Copy link
Author

akter-sust commented Oct 27, 2019

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') 

@yilei
Copy link
Contributor

yilei commented Oct 28, 2019

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.

@akter-sust
Copy link
Author

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') 

@stefanistrate
Copy link

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.

Hi @yilei! Is this behaviour referenced anywhere in the abseil documentation? Thanks!

@yilei
Copy link
Contributor

yilei commented Jul 13, 2021

@stefanistrate This is only mentioned in the --verbosity flag's help string.

@eshijia
Copy link

eshijia commented Nov 27, 2021

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.

@yilei
Copy link
Contributor

yilei commented Nov 29, 2021

@eshijia do you have a minimal but complete reproducible example? snippets aren't great since how/when they are executed is important.

@eshijia
Copy link

eshijia commented Nov 30, 2021

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')

@wpopielarski
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants