Commit e85a86e4 authored by Jonathan Schaeffer's avatar Jonathan Schaeffer
Browse files

Simplify logging system

parent 8db5bec9
#!/bin/env python
import logging.config
import logging
import os
import sys
import subprocess
from time import gmtime, strftime
from datetime import datetime, date, timedelta
import yaml
from influxdb import InfluxDBClient
from datetime import datetime, date, timedelta
import psycopg2
import click
from fdsnextender import FdsnExtender
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
def validate_config(cfg):
"""
Validate configuration
"""
# Logger file exists and is readable ?
return True
def dict_dumper(dict):
try:
return dict.toJSON()
except:
return dict.__dict__
def scan_volume(path):
"""
Scanne un volume indiqué par son chemin (path).
......@@ -91,24 +76,22 @@ def scan_volumes(volumes):
@click.option('--config-file', 'configfile', type=click.File(), help='Configuration file path', envvar='CONFIG_FILE', show_default=True,
default=f"{os.path.dirname(os.path.realpath(__file__))}/config.yml")
@click.option('--force-scan', flag_value=True, default=False, help='Force scanning of the archive')
def cli(configfile, force_scan):
@click.option('--dryrun', flag_value=True, default=False, help="Do not send metrics to database")
@click.option("--verbose", flag_value=True, default=False, help="Verbose mode")
def cli(configfile, force_scan, dryrun, verbose):
"""
Command line interface. Stands as main
"""
if verbose:
logger.setLevel(logging.DEBUG)
logger.info("Starting")
try:
cfg = yaml.load(configfile, Loader=yaml.SafeLoader)
except:
print(f"Error reading file {configfile}")
validate_config(cfg)
# At this point we ensure that configuration is sane.
logger_file = cfg['logger_file']
if not logger_file.startswith('/'):
logger_file = os.path.split(configfile.name)[0]+'/'+logger_file
logging.config.fileConfig(logger_file)
logger = logging.getLogger("resif_data_reporter")
logger.info("Starting")
statistics = []
today = date.today().strftime("%Y-%m-%d")
......@@ -156,6 +139,9 @@ def cli(configfile, force_scan):
except:
logger.error("Error writing data to cache")
if dryrun:
logger.info("Dryrun mode, exit")
sys.exit(0)
# Write to postgres database
if 'postgres' in cfg:
logger.info('Writing to postgres database')
......@@ -220,3 +206,6 @@ def cli(configfile, force_scan):
except Exception as e:
logger.error("Unexpected error writing data to influxdb")
logger.error(e)
if __name__ == "__main__":
cli()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment