-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logutil: move logging bits to a new logutil module in ohmu_common_py
- Loading branch information
Showing
6 changed files
with
74 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copied from https://github.com/ohmu/ohmu_common_py ohmu_common_py/logutil.py version 0.0.1-0-unknown-fa54b44 | ||
""" | ||
pglookout - logging formats and utility functions | ||
Copyright (c) 2015 Ohmu Ltd | ||
See LICENSE for details | ||
""" | ||
|
||
import logging | ||
import logging.handlers | ||
import os | ||
|
||
try: | ||
from systemd import daemon # pylint: disable=no-name-in-module | ||
except ImportError: | ||
daemon = None | ||
|
||
|
||
LOG_FORMAT = "%(asctime)s\t%(name)s\t%(threadName)s\t%(levelname)s\t%(message)s" | ||
LOG_FORMAT_SHORT = "%(levelname)s\t%(message)s" | ||
LOG_FORMAT_SYSLOG = "%(name)s %(threadName)s %(levelname)s: %(message)s" | ||
|
||
|
||
def set_syslog_handler(address, facility, logger): | ||
syslog_handler = logging.handlers.SysLogHandler(address=address, facility=facility) | ||
logger.addHandler(syslog_handler) | ||
formatter = logging.Formatter(LOG_FORMAT_SYSLOG) | ||
syslog_handler.setFormatter(formatter) | ||
return syslog_handler | ||
|
||
|
||
def configure_logging(level=logging.DEBUG, short_log=False): | ||
# Are we running under systemd? | ||
if os.getenv("NOTIFY_SOCKET"): | ||
logging.basicConfig(level=level, format=LOG_FORMAT_SYSLOG) | ||
if not daemon: | ||
print( | ||
"WARNING: Running under systemd but python-systemd not available, " | ||
"systemd won't see our notifications" | ||
) | ||
else: | ||
logging.basicConfig(level=level, format=LOG_FORMAT_SHORT if short_log else LOG_FORMAT) | ||
|
||
|
||
def notify_systemd(status): | ||
if daemon: | ||
daemon.notify(status) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
pglookout - test configuration | ||
Copyright (c) 2016 Ohmu Ltd | ||
See LICENSE for details | ||
""" | ||
|
||
from pglookout import logutil | ||
|
||
|
||
logutil.configure_logging() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters