[ckan-dev] Logging in commands question

David Read david.read at okfn.org
Wed Apr 6 15:02:06 UTC 2011


Adria,

I'm sure you're aware, but just to be ultra-clear, we normally do for
logging is initialise it at the top of every python file:

log = __import__("logging").getLogger(__name__)

and call it directly:

def my_func():
    log.info("Starting")
    ...
    log.info("Finished")

But maybe you have another reason for wanting indirection through your
function? In which case, you could try to put the function in a
separate python file and all clients import that. I think this would
avoid the problems with ordering of imports and setup of module
variables that I think are shown in your examples.
http://effbot.org/pyfaq/how-do-i-share-global-variables-across-modules.htm

But I could be wrong...

David

On 6 April 2011 16:37, William Waites <ww at styx.org> wrote:
> I don't know the exact answer. It probably has something to do with
> the fact that paste.Command (of which CkanCommand is a subclass)
> initialised logging according to the config file. If the logger
> reference obtained before the logging is initialised I think it may
> just log into the bit bucket.
>
> Evils of global variables which the whole paste/pylons stack is
> riddled with.
>
> Cheers,
> -w
>
> * [2011-04-06 15:31:29 +0100] Adrià Mercader <amercadero at gmail.com> écrit:
>
> ] Hi all,
> ]
> ] Sorry if this is a silly question, but I haven't managed to solve it.
> ] When implementing a CLI command from an extension, I'm having problems
> ] to get the log messages to show in the console.
> ]
> ] In ckan/myextension/echo.py:
> ]
> ] log = __import__("logging").getLogger(__name__)
> ] def echo_log():
> ]     log.info("That's a message")
> ]
> ] def another_function():
> ]     pass
> ]
> ] Now, if import the echo function outside the command class, the log
> ] messages don't come up in the command file
> ]
> ] from ckan.lib.cli import CkanCommand
> ] from ckan.myextension.echo import echo_log
> ]
> ] class TestCommand(CkanCommand)
> ]
> ]     def command():
> ]         echo_log() #Does not output anything in the command line
> ]
> ] But if import the function within the function, the log messages are shown:
> ]
> ] from ckan.lib.cli import CkanCommand
> ]
> ] class TestCommand(CkanCommand)
> ]
> ]     def command():
> ]         from ckan.myextension.echo import echo_log
> ]         echo_log() #Does output the log message
> ]
> ]
> ] And to make matters worse, if I import another function of the same
> ] module outside the command class, messages are not shown
> ]
> ] from ckan.lib.cli import CkanCommand
> ] from ckan.myextension.echo import another_function
> ] class TestCommand(CkanCommand)
> ]
> ]     def command():
> ]         from ckan.myextension.echo import echo_log
> ]         echo_log() #Does not output anything in the command line
> ]
> ] As far as I know, this does not happen with normal python classes so
> ] maybe I'm missing something with CkanCommand?
> ]
> ] Thanks,
> ]
> ] Adrià
> ]
> ] _______________________________________________
> ] ckan-dev mailing list
> ] ckan-dev at lists.okfn.org
> ] http://lists.okfn.org/mailman/listinfo/ckan-dev
>
> --
> William Waites                <mailto:ww at styx.org>
> http://river.styx.org/ww/        <sip:ww at styx.org>
> F4B3 39BF E775 CF42 0BAB  3DF0 BE40 A6DF B06F FD45
>
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> http://lists.okfn.org/mailman/listinfo/ckan-dev
>




More information about the ckan-dev mailing list