Logging - how to enable debug logging

587 views
Skip to first unread message

Milan Leszkow

unread,
Oct 15, 2019, 10:08:49 AM10/15/19
to asyncssh-users
Hello,
could you point me how to enable logging for sftp_server?

I wish to have DEBUG log enabled. Where to put call to set log level?

We can demonstrate it on example: https://asyncssh.readthedocs.io/en/latest/index.html#sftp-server

I tried to put

asyncssh.set_log_level('DEBUG')
asyncssh.set_sftp_log_level('DEBUG')
asyncssh.set_debug_level(3)

right after import, but still only Warning level is set.




Ron Frederick

unread,
Oct 15, 2019, 10:28:39 AM10/15/19
to Milan Leszkow, asyncssh-users
Before you can use Python’s “logging” package, you need to initialize it. Then, you can make the calls you have above. The simplest way to initialize it is to call:

logging.basicConfig()

Without this, the “lastReort” handler is used, which only outputs things of WARNING or greater.
--
Ron Frederick
ro...@timeheart.net



Milan Leszkow

unread,
Oct 15, 2019, 1:59:53 PM10/15/19
to asyncssh-users
Thank you,
works like a charm!

Dne úterý 15. října 2019 16:28:39 UTC+2 Ron Frederick napsal(a):

Milan Leszkow

unread,
Nov 18, 2019, 8:25:25 AM11/18/19
to asyncssh-users
It's me again with logging.
Now i need to enable globlkay logging to Roatting File handler.
How-to  with asyncssh? Where and hoiw to place setting for such handler?


Thank you.


Dne úterý 15. října 2019 19:59:53 UTC+2 Milan Leszkow napsal(a):

Milan Leszkow

unread,
Nov 18, 2019, 8:35:08 AM11/18/19
to asyncssh-users
Let's say, I need all events write t file, not to stdout.


Dne pondělí 18. listopadu 2019 14:25:25 UTC+1 Milan Leszkow napsal(a):

Ron Frederick

unread,
Nov 19, 2019, 10:13:18 PM11/19/19
to Milan Leszkow, asyncssh-users
If you want the log output to go to a file, you can pass a filename in on the logging.basicConfig() call. See https://docs.python.org/3/library/logging.html for more details. Specifically, check out the “filename” and “filemode” arguments. You may also want to adjust the format of the output with the “format” argument.

If you need something other than a file to be called when new log data is generated, check out the “stream” argument, or you can consider passing in handler functions via the “handlers” argument.

I don’t know of anything automatic that will do file rotation for you in the standard logging library. You’d have to probably use a stream or handler for that, and write the data to files yourself, rotating files when necessary. Whatever you do here isn’t really specific to AsyncSSH In any way, though.

Milan Leszkow

unread,
Nov 20, 2019, 6:55:23 AM11/20/19
to asyncssh-users
Hello,
yes I tried to use basicConfig. But I don't know how to setup logging in global scope.
let's say I need to assign another handler to asyncssh.logger to send messages also to that handler.



Dne středa 20. listopadu 2019 4:13:18 UTC+1 Ron Frederick napsal(a):

Ron Frederick

unread,
Nov 21, 2019, 1:48:17 AM11/21/19
to Milan Leszkow, asyncssh-users
If you don’t want warnings & errors to get logged to stdout, I think you need to set up something at the global level, either with logging.basicConfig() or other calls on logger objects you get back from logging.getLogger(). All of this is explained in detail at https://docs.python.org/3/library/logging.html — see the links at the top there to the “Basic Tutorial”, “Advanced Tutorial”, and “Logging Cookbook”.

If you just want to change the configuration for AsyncSSH’s logging, you can either call getLogger(‘asyncssh’) to get its logger object, or just refer to the already created asyncssh.logger object. There’s also a separate SFTP logger object registered under the name ‘asyncssh.sftp’, and available as asyncssh.sftp_logger. Any changes made to the main AsyncSSH logger affect both, but if you like you set a different log level or debug level for just SFTP with that logger object.

If you want to add a handler specifically for AsyncSSH log messages, you can call addHandler() on these logger objects. You can also filter what gets logged by adding a filter with addFilter(). 

Milan Leszkow

unread,
Nov 21, 2019, 3:42:24 AM11/21/19
to asyncssh-users
Thank you very much for guiding me. Logging module is sometimes magic for me.

This is working how I wants:

create handler for Timed rotation with formatter:

loghd = logging.handlers.TimedRotatingFileHandler(filename="/tmp/log.log", when='D', backupCount=7)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
loghd.setFormatter(formatter)

then get loggers:

logger = logging.getLogger('asyncssh')
logger_subsys = logging.getLogger('asyncssh.sftp')
logger.addHandler(loghd)
logger_subsys.addHandler(loghd)

logger.propagate = False # disable output to stdout
asyncssh.logging.set_log_level("DEBUG")
asyncssh.logging.set_sftp_log_level("DEBUG")
asyncssh.logging.set_debug_level(1)


now all messages go to file.

Dne čtvrtek 21. listopadu 2019 7:48:17 UTC+1 Ron Frederick napsal(a):

Ron Frederick

unread,
Nov 21, 2019, 10:20:17 AM11/21/19
to Milan Leszkow, asyncssh-users
Great - glad you got it working! I actually didn’t realize that Python’s logging module had support for file rotation available. I’ll have to keep that in mind in the future!

One small thing I’d recommend changing — the set_log_level(), set_sftp_log_level(), and set_debug_level() functions in asyncssh.logging are actually exported at the asyncssh level as well, so you can shorten the last few lines to:

asyncssh.set_log_level("DEBUG")
asyncssh.set_sftp_log_level("DEBUG")
asyncssh.set_debug_level(1)

Also, since the log level is inherited, you can actually leave out the “asyncssh.set_sftp_log_level” call if you want SFTP to use the same log level as other AsyncSSH messages. You can also leave out the “asyncssh.set_debug_level” call if you want level 1 for that, as that’s already the default.

Finally, Python’s logging code recommends against setting the same log handler at two different levels in the hierarchy. If you do that, you’ll get the same log messages twice. So, you should probably remove the “logger_subsys.addHandler” call, which means you may not need to set “logger_subsys” at all.
Reply all
Reply to author
Forward
0 new messages