salt icon indicating copy to clipboard operation
salt copied to clipboard

Importing salt.config affects logging module

Open takeda opened this issue 9 years ago • 6 comments

The following code prints "test" when salt.config line is commented out, and doesn't print anything if it isn't:

import logging
import salt.config

log = logging.getLogger(__name__)
log.error("test")

Importing a package should have no side effects, especially packages that are meant to be imported by other code.

takeda avatar Feb 14 '17 06:02 takeda

@takeda how are you calling this code.

I'm having a hard time replicating this when adding this code as an execution module like so:

import logging
#import salt.config

log = logging.getLogger(__name__)
log.error("test")

def test():
    log.error("test2")

And when I run that it always adds both "test" and "test2" to the minion log file whether I comment out salt.config or not.

[root@19771408a47b /]# tail -f /var/log/salt/minion   | grep -i test
2017-02-14 15:26:02,713 [salt.minion                              ][DEBUG   ][151] Command details {'tgt_type': 'glob', 'jid': '20170214152602711721', 'tgt': '*', 'ret': '', 'user': 'root', 'arg': [], 'fun': 'testissue.test'}
2017-02-14 15:26:02,722 [salt.loader.localhost.ext.module.testissue][ERROR   ][613] test
2017-02-14 15:26:02,722 [salt.utils.lazy                          ][DEBUG   ][613] LazyLoaded testissue.test
2017-02-14 15:26:02,723 [salt.loader.localhost.ext.module.testissue][ERROR   ][613] test2

Maybe i'm misunderstanding how you are calling this?

Ch3LL avatar Feb 14 '17 15:02 Ch3LL

Oh and I am syncing that as a custom execution module:

[root@19771408a47b /]# ls /srv/salt/_modules/
testissue.py

And calling it like so salt '*' testissue.test

Ch3LL avatar Feb 14 '17 15:02 Ch3LL

The scenario I'm describing is a standard python script that's run using python test.py not as a salt module.

takeda avatar Feb 14 '17 17:02 takeda

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

stale[bot] avatar Sep 07 '18 18:09 stale[bot]

Ken Jorden from SaltStack found the trick to make it work:

Make one use of log before import salt.config

import logging
log = logging.getLogger(__name__)
log.error("test")

import salt.config
log.error("test 02")

gaoweisgp avatar Mar 21 '19 16:03 gaoweisgp

Thank you, since I needed to make it work I figured out as much.

The problem is still that import causes side effects. Just because Python allows that doesn't mean it should be done. We should follow the principle of least surprprise.

Since this is related to logging, let's look how that's done in the built-in module. Importing it doesn't do any action that would be unexpected but the user. After import user has an option to call basicConfig() or configure the module themselves. The basicConfig() is where the side effect is happening. Why not do the same in salt? Make salt.config only import functions and then user can call salt.config.setup_logging()

takeda avatar Mar 21 '19 18:03 takeda