Commit af249dfc authored by Bram Daams's avatar Bram Daams
Browse files

default log level ERROR

parent e2d2bbcb
...@@ -6,6 +6,9 @@ __pycache__/ ...@@ -6,6 +6,9 @@ __pycache__/
# C extensions # C extensions
*.so *.so
# config file
sch.conf
# Distribution / packaging # Distribution / packaging
.Python .Python
env/ env/
...@@ -102,4 +105,4 @@ ENV/ ...@@ -102,4 +105,4 @@ ENV/
.mypy_cache/ .mypy_cache/
# IDE settings # IDE settings
.vscode/ .vscode/
\ No newline at end of file
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
A cron shell wrapper for registering and updating cron jobs automatically in A cron shell wrapper for registering and updating cron jobs automatically in
[healthchecks](https://healthchecks.io) or your [own hosted copy of Healthchecks](https://github.com/healthchecks/healthchecks). [healthchecks](https://healthchecks.io) or your [own hosted copy of Healthchecks](https://github.com/healthchecks/healthchecks).
> warning: this software package should be considered "alpha" > WARNING: once setup and configured, the code in this package runs as user specified in the cron jobs and is wrapped around the cron job commands. Errors in this package could prevent your cron jobs from being executed.
## Installation ## Installation
...@@ -76,10 +76,16 @@ Create a configuration file `/etc/sch.conf` that looks like: ...@@ -76,10 +76,16 @@ Create a configuration file `/etc/sch.conf` that looks like:
healthchecks_api_url = https://hc.example.com/api/v1/ healthchecks_api_url = https://hc.example.com/api/v1/
healthchecks_api_key = xxmysecretkeyxx healthchecks_api_key = xxmysecretkeyxx
``` ```
And fill in the API URL and the key obtained from the Healthchecks project And fill in the API URL and the key obtained from the Healthchecks project
settings block labeled "API Access". settings block labeled "API Access".
Optionally, specify the log level in the configuration file:
``` ini
[sch]
loglevel = DEBUG
```
Possible values for loglevel are explained [here](https://docs.python.org/3/library/logging.html#levels). The default log level is `ERROR`.
## Monitoring cron jobs ## Monitoring cron jobs
Just decorate your existing cron tabs by specifying the alternative `sch`: Just decorate your existing cron tabs by specifying the alternative `sch`:
``` ```
......
...@@ -22,6 +22,23 @@ import tzlocal ...@@ -22,6 +22,23 @@ import tzlocal
from crontabs import CronTabs from crontabs import CronTabs
from . import __version__ from . import __version__
def get_config():
"""
try loading the configuration file
"""
try:
my_config = configparser.ConfigParser()
my_config.read(['sch.conf', '/etc/sch.conf'])
except configparser.Error:
logging.error(
'Could not find/read/parse config'
'file sch.conf or /etc/sch.conf'
)
my_config = None
return my_config
HANDLER = logging.handlers.SysLogHandler('/dev/log') HANDLER = logging.handlers.SysLogHandler('/dev/log')
FORMATTER = logging.Formatter( FORMATTER = logging.Formatter(
'{name}/%(module)s.%(funcName)s:' '{name}/%(module)s.%(funcName)s:'
...@@ -29,8 +46,13 @@ FORMATTER = logging.Formatter( ...@@ -29,8 +46,13 @@ FORMATTER = logging.Formatter(
) )
HANDLER.setFormatter(FORMATTER) HANDLER.setFormatter(FORMATTER)
ROOT = logging.getLogger() ROOT = logging.getLogger()
# log level DEBUG CONFIG = get_config()
ROOT.setLevel(logging.DEBUG) try:
LEVEL = CONFIG.get('sch', 'loglevel')
ROOT.setLevel(LEVEL)
except configparser.Error:
ROOT.setLevel(logging.ERROR)
ROOT.addHandler(HANDLER) ROOT.addHandler(HANDLER)
...@@ -81,10 +103,8 @@ def get_hc_api(): ...@@ -81,10 +103,8 @@ def get_hc_api():
try loading Healthchecks API url and key try loading Healthchecks API url and key
and return an instance of Healthchecks or None if it failed and return an instance of Healthchecks or None if it failed
""" """
config = get_config()
try: try:
config = configparser.ConfigParser()
config.read(['sch.conf', '/etc/sch.conf'])
url = config.get('hc', 'healthchecks_api_url') url = config.get('hc', 'healthchecks_api_url')
key = config.get('hc', 'healthchecks_api_key') key = config.get('hc', 'healthchecks_api_key')
...@@ -97,7 +117,6 @@ def get_hc_api(): ...@@ -97,7 +117,6 @@ def get_hc_api():
except configparser.Error: except configparser.Error:
logging.error( logging.error(
'Could not find/read/parse config' 'Could not find/read/parse config'
'file sch.conf or /etc/sch.conf'
) )
healthchecks = None healthchecks = None
...@@ -195,7 +214,7 @@ def shell(command): ...@@ -195,7 +214,7 @@ def shell(command):
start_time = time.time() start_time = time.time()
# execute command # execute command
logging.debug( logging.info(
"Executing shell commmand: %s (job.id=%s)", "Executing shell commmand: %s (job.id=%s)",
command, command,
job.id, job.id,
......
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