From 5da99f636f39afea83ea5373847bcfb499c6000d Mon Sep 17 00:00:00 2001 From: Simon Fonteneau Date: Tue, 3 Sep 2024 21:53:14 +0200 Subject: [PATCH] edit log format (json) --- README.md | 2 +- gaps.conf.template | 2 +- gapslib.py | 17 ++++++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8f1ce15..7c49b37 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ If you are not under debian or if you do not have the packages available : 1. apt-get install python3-pip 2. pip3 install -r /opt/samba4-gaps/requirements.txt -show log application in syslog +show log in /var/log/samba4-gaps.log in json format * If you are having issues with Google Permissions - you might need to add domain-wide authority to your service Delegate domain-wide authority to your service account https://developers.google.com/drive/web/delegation#delegate_domain-wide_authority_to_your_service_account diff --git a/gaps.conf.template b/gaps.conf.template index 0a4f18d..da9d2b0 100644 --- a/gaps.conf.template +++ b/gaps.conf.template @@ -2,7 +2,7 @@ domain = yourdomain.com replace_domain = false dbpath=/opt/samba4-gaps/last_send.db - +logfile = /var/log/samba4-gaps.log [google] admin_email = adminuser@yourdomain.com diff --git a/gapslib.py b/gapslib.py index 1d5684f..6dd5ca4 100644 --- a/gapslib.py +++ b/gapslib.py @@ -1,13 +1,17 @@ #!/usr/bin/env python import sys import hashlib -import syslog import json import ldb import os import optparse import hashlib import pickle +import logging +import datetime +import json + +logger = logging.getLogger() try: from Cryptodome import Random @@ -39,6 +43,10 @@ config = configparser.ConfigParser() config.read('/etc/gaps/gaps.conf') +logfile = config.get('common', 'logfile') +fhandler = logging.FileHandler(logfile) +logger.addHandler(fhandler) + db = SqliteDatabase(config.get('common', 'dbpath')) def hash_for_data(data): @@ -86,7 +94,7 @@ def update_password(mail, pwd, sha1hashnt): try: user = service.users().get(userKey = mail).execute() except: - syslog.syslog(syslog.LOG_WARNING, '[WARNING] Account %s not found' % mail) + logger.warning(json.dumps({'status':"warning",'msg':'Account not found','timestamp': str(datetime.datetime.utcnow()),'mail':mail})) return 0 try: @@ -96,10 +104,9 @@ def update_password(mail, pwd, sha1hashnt): response = request.execute() LastSend.insert(mail=mail,sha1hashnt = sha1hashnt).on_conflict_replace().execute() - syslog.syslog(syslog.LOG_WARNING, '[NOTICE] Updated password for %s' % mail) - + logger.info(json.dumps({'status':"ok",'msg':'updated password','timestamp': str(datetime.datetime.utcnow()),'mail':mail})) except Exception as e: - syslog.syslog(syslog.LOG_WARNING, '[ERROR] %s : %s' % (mail,str(e))) + logger.error(json.dumps({'status':"error",'msg':str(e),'timestamp': str(datetime.datetime.utcnow()),'mail':mail})) finally: service = None