improve logging config
This commit is contained in:
40
README.md
40
README.md
@@ -178,35 +178,33 @@ pyinotifyd.add_watch(
|
|||||||
|
|
||||||
## Logging
|
## Logging
|
||||||
Pythons [logging](https://docs.python.org/3/howto/logging.html) framework is used to log messages (see https://docs.python.org/3/howto/logging.html).
|
Pythons [logging](https://docs.python.org/3/howto/logging.html) framework is used to log messages (see https://docs.python.org/3/howto/logging.html).
|
||||||
|
The following loglevels are available:
|
||||||
Configure the global loglevel. This is the default:
|
* DEBUG
|
||||||
|
* INFO
|
||||||
|
* WARNING
|
||||||
|
* ERROR
|
||||||
|
* CRITICAL
|
||||||
```python
|
```python
|
||||||
logging.getLogger().setLevel(logging.WARNING)
|
# Configure global loglevel
|
||||||
|
setLoglevel(INFO)
|
||||||
```
|
```
|
||||||
It is possible to configure the loglevel per *logname*. This is an example for logname **sched**:
|
Configure loglevel per *logname*.
|
||||||
```python
|
```python
|
||||||
logging.getLogger("sched").setLevel(logging.INFO)
|
setLoglevel(INFO, logname="daemon")
|
||||||
```
|
```
|
||||||
|
|
||||||
### Syslog
|
### Syslog
|
||||||
Add this to your config file to send log messages to a local syslog server.
|
Send log messages to the local syslog server.
|
||||||
```python
|
```python
|
||||||
# send log messages to the Unix socket of the syslog server.
|
# Enable logging to local syslog server (/dev/log). Use *address* to specify a different target.
|
||||||
syslog = logging.handlers.SysLogHandler(
|
enableSyslog(
|
||||||
address="/dev/log")
|
loglevel=INFO,
|
||||||
|
address="/dev/log")
|
||||||
|
|
||||||
# set the log format of syslog messages
|
# Enable syslog per logname
|
||||||
log_format = "pyinotifyd/%(name)s: %(message)s"
|
enableSyslog(
|
||||||
syslog.setFormatter(logging.Formatter(log_format))
|
loglevel=INFO,
|
||||||
|
name="daemon")
|
||||||
# set the log level for syslog messages
|
|
||||||
syslog.setLevel(logging.INFO)
|
|
||||||
|
|
||||||
# enable syslog for pyinotifyd
|
|
||||||
logging.getLogger().addHandler(syslog)
|
|
||||||
|
|
||||||
# or enable syslog per logname
|
|
||||||
#logging.getLogger("logname").addHandler(syslog)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
|
|||||||
@@ -37,6 +37,22 @@ from pyinotifyd.scheduler import TaskScheduler, Cancel
|
|||||||
__version__ = "0.0.3"
|
__version__ = "0.0.3"
|
||||||
|
|
||||||
|
|
||||||
|
def setLoglevel(loglevel, logname=None):
|
||||||
|
logger = logging.getLogger(logname)
|
||||||
|
logger.setLevel(loglevel)
|
||||||
|
|
||||||
|
|
||||||
|
def enableSyslog(loglevel=None, address="/dev/log", logname=None):
|
||||||
|
logger = logging.getLogger(logname)
|
||||||
|
syslog = logging.handlers.SysLogHandler(address=address)
|
||||||
|
syslog.setFormatter(
|
||||||
|
logging.Formatter(f"{Pyinotifyd.name}/%(name)s: %(message)s"))
|
||||||
|
if loglevel:
|
||||||
|
syslog.setLevel(loglevel)
|
||||||
|
|
||||||
|
logger.addHandler(syslog)
|
||||||
|
|
||||||
|
|
||||||
class _SchedulerList:
|
class _SchedulerList:
|
||||||
def __init__(self, schedulers=[], loop=None):
|
def __init__(self, schedulers=[], loop=None):
|
||||||
if not isinstance(schedulers, list):
|
if not isinstance(schedulers, list):
|
||||||
@@ -184,8 +200,10 @@ class Pyinotifyd:
|
|||||||
def from_cfg_file(config_file):
|
def from_cfg_file(config_file):
|
||||||
config = {}
|
config = {}
|
||||||
name = Pyinotifyd.name
|
name = Pyinotifyd.name
|
||||||
exec("import logging", {}, config)
|
exec("from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL",
|
||||||
|
{}, config)
|
||||||
exec(f"from {name} import Pyinotifyd, Watch", {}, config)
|
exec(f"from {name} import Pyinotifyd, Watch", {}, config)
|
||||||
|
exec(f"from {name} import setLoglevel, enableSyslog", {}, config)
|
||||||
exec(f"from {name}.scheduler import *", {}, config)
|
exec(f"from {name}.scheduler import *", {}, config)
|
||||||
with open(config_file, "r") as fh:
|
with open(config_file, "r") as fh:
|
||||||
exec(fh.read(), {}, config)
|
exec(fh.read(), {}, config)
|
||||||
|
|||||||
@@ -82,3 +82,27 @@
|
|||||||
# event_map = event_map,
|
# event_map = event_map,
|
||||||
# rec=True,
|
# rec=True,
|
||||||
# auto_add=True)
|
# auto_add=True)
|
||||||
|
|
||||||
|
|
||||||
|
################
|
||||||
|
# Log config #
|
||||||
|
################
|
||||||
|
|
||||||
|
# set global loglevel
|
||||||
|
#setLoglevel(DEBUG)
|
||||||
|
|
||||||
|
# set loglevel per logname
|
||||||
|
#setLoglevel(
|
||||||
|
# DEBUG,
|
||||||
|
# logname="daemon")
|
||||||
|
|
||||||
|
# enable syslog
|
||||||
|
#enableSyslog(
|
||||||
|
# loglevel=DEBUG,
|
||||||
|
# address="/dev/log")
|
||||||
|
|
||||||
|
# enable syslog per logname
|
||||||
|
#enableSyslog(
|
||||||
|
# loglevel=DEBUG,
|
||||||
|
# address="/dev/log",
|
||||||
|
# logname="sched")
|
||||||
|
|||||||
Reference in New Issue
Block a user