#Logging using the python SDK

1 messages · Page 1 of 1 (latest)

limber wolf
#

Trying to debug steps using classic python loggin stuff
And found that link from the docs: https://github.com/dagger/dagger/blob/main/sdk/python/src/dagger/log.py

I'm not sucessful using it. I'm wondering why we can just do something like bellow :

import dagger
from dagger import dag, function, object_type
from dagger.log import configure_logging, logging
import dagger.log

configure_logging()

@object_type
class Hook2Mail:
    @function
    async def simple(self) -> str:
        logging.debug("test4")
        logging.info("test3")
        logging.warning("test2")
        logging.error("test1")
        return "hello"

It does "run" but there is no log message displayed when running with `--progress=plain``

GitHub

An engine to run your pipelines in containers. Contribute to dagger/dagger development by creating an account on GitHub.

#

print does work but not so handy as logger can be

agile moss
#

On my own I'm just using print statements and run dagger call with the -v flag.

The most important thing is your return statement. You can use a string variable and for instance concatenate log infos in your variable and return it.

But you already know this I think.

limber wolf
#

thxs for replying @agile moss
yes i was aware of the return and print that could be handy. just looking for the "best-practice" (or the python way) solution here

dry shoal
#

That configure_logging is used to setup a logging.StreamHandler for the dagger client library, which his useful to debug the SDK.

#

You need to configure a handler for your own package.

#

Using otel log should also work, but haven't tried it yet. This would be the preferred way.

limber wolf
#

@dry shoal thanxs for reply
Do you have an example of that ? tried many thing but did not find a way to log anything that will be displayed when calling dagger call simple --progress=plain

#

(except 'hello' of course)

dry shoal
# limber wolf <@768585883120173076> thanxs for reply Do you have an example of that ? tried ma...

Sure, let's start with Python stdlib:

  1. Use your own logger instead of the root logger, otherwise you'll get too much stuff.
  2. Lower the logging level to logging.DEBUG otherwise you'll only see logs from warning up (the default).
  3. Add the logging.StreamHandler() so it prints to sys.stderr (can set it to sys.stdout if you want).
import logging
import dagger

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())


@dagger.object_type
class Hook2Mail:
    @dagger.function
    def simple(self) -> str:
        logger.debug("test4")
        logger.info("test3")
        logger.warning("test2")
        logger.error("test1")
        return "hello"