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 cron option to suppress logging to stdout #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ How to configure config.json
{
"log": "/tmp/gitlab-ldap-sync.log", // Where to store the log file. If not set, will log to stdout
"log_level": "INFO", // The log level
"cron": false, // Should the script run as cronjob suppressing any output to stdout
"gitlab": {
"api": "https://gitlab.example.com", // Url of your GitLab
"private_token": "xxxxxxxxxxxxxxxxxxxx", // Token generated in GitLab for an user with admin access
Expand Down Expand Up @@ -129,4 +130,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file

## Acknowledgments

* Hat tip to anyone whose code was used
* Hat tip to anyone whose code was used
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"log": "/tmp/gitlab-ldap-sync.log",
"log_level": "INFO",
"cron": false,
"gitlab": {
"api": "",
"private_token": "",
Expand Down
21 changes: 17 additions & 4 deletions gitlab-ldap-sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@
import ldap.asyncsearch
import logging

CRON = False

def logstdout(s):
if not CRON:
print(s)

if __name__ == "__main__":
print('Initializing gitlab-ldap-sync.')
config = None
Comment on lines +11 to 18
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a global CRON in both logstdout and __main__ to ensure we use the same variable

with open('config.json') as f:
config = json.load(f)
if config is None:
logstdout('Initializing gitlab-ldap-sync failed.')
Comment on lines +21 to +22
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be within the else at the end of this script

if config is not None:
print('Done.')
print('Updating logger configuration')
logstdout('Initializing gitlab-ldap-sync successfull.')
if config['cron']:
CRON = True
logstdout('Updating logger configuration')
Comment on lines +24 to +27
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consitent this should be something like this :

        CRON = config['cron']
        logstdout('Initializing gitlab-ldap-sync successfull.')
        logstdout('Updating logger configuration')

log_option = {
'format': '[%(asctime)s] [%(levelname)s] %(message)s'
}
Expand All @@ -24,7 +33,11 @@
if config['log_level']:
log_option['level'] = getattr(logging, str(config['log_level']).upper())
logging.basicConfig(**log_option)
print('Done.')
if config['cron'] and config['log']:
console = logging.StreamHandler()
console.setLevel(logging.ERROR)
logging.getLogger('').addHandler(console)
Comment on lines +39 to +42
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this part. We already suppress "print" logs and you can set the log level in config.json

logstdout('Done.')
logging.info('Connecting to GitLab')
if config['gitlab']['api']:
gl = None
Expand Down