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

Add the possibity to log in both STDOUT and a file-object, with different formats. #128

Open
rdebroiz opened this issue Mar 6, 2018 · 7 comments

Comments

@rdebroiz
Copy link

rdebroiz commented Mar 6, 2018

What I'd like to do is:

  • logging in both a file-like object and STDOUT.
  • have control on the format of the messages. (change it depending on the destination, the level of log, etc.)

I can't figure how to achieve it with the current API.
Is it something that goes against the philosophy of txaio or do you plan to have such features one day?

@meejah
Copy link
Contributor

meejah commented Mar 6, 2018

We would like to inter-operate with logging nicely. When using Twisted, the above would be accomplished with "observers". In stdlib logging these are "handlers" (approximately?).

As per #autobahn IRC conversation, it seems that txaio adds a root-level handler in start_logging that makes the above use-case hard to achieve (is that about right?)

@rdebroiz
Copy link
Author

rdebroiz commented Mar 6, 2018

Well when you use the txaio API you do not have access to the low-level object used internally by the std-lib or twisted logging module to configure it as you want.

What I'd like I guess is an extended API in txaio that give the possibility to add an object interfaced on an observer/handler depending if you are using twisted or asyncio to the logger that you get when you call txaio.make_logger()

This object would allow you to configure the format and the destination used by the logger.

I see that you can pass an observer to the twisted implementation of make_logger

# tx.py
def make_logger(level=None, logger=_Logger, observer=None):

Maybe it's a first step in that direction? I haven't dig enough into the code and i'm not familiar with twisted logging.

@meejah
Copy link
Contributor

meejah commented Mar 6, 2018

I think maybe what you want to do is this:

  • not call start_logging at all
  • set up your own handlers (or observers for Twisted)
  • call txaio's set_global_log_level() method
  • start the global logging thing (beginLoggingTo(observers) in Twisted)

If that does work for your use-case, the remaining part will be to ensure that txaio functions properly (and the logs are emitted correctly) if you call ONLY set_global_log_level. That's the piece that turns "not the right level" calls into no-ops, which is fairly critical for Autobahn (because we want "trace" and "debug" statements to be no or extremely-little overhead).

@rdebroiz
Copy link
Author

rdebroiz commented Mar 6, 2018

It is pretty much what i did.

I do not call set_global_log_level but ApplicationRunner.run(ApplicationSession, log_level=''my_level')

but as i pointed here crossbario/autobahn-python#952, the problem i face then is that a handler is still created by autobahn somewhere, so every records are logged two times. everything seems to work fine otherwise, even if I removed the created handler.

@meejah
Copy link
Contributor

meejah commented Mar 6, 2018

Yes, ApplicationRunner currently calls start_logging -- can you try either with the 'component' API or by not using .run() (e.g. use .start() which doesn't set up logging for you). Or by just not using Autobahn at all for testing this method?

@rdebroiz
Copy link
Author

rdebroiz commented Mar 7, 2018

I now use ApplicationRunner.run(ApplicationSession, start_loop=False), and launch the loop myself, so i can prevent the call to txaio.start_logging

If I call txaio.set_global_log_level it works like a charm thanks!

@meejah
Copy link
Contributor

meejah commented Mar 7, 2018

Okay, so I think we should document that better -- basically "how to do your own logging stuff"

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

No branches or pull requests

2 participants