Last active
February 28, 2024 12:35
-
-
Save danielkraic/a1657f19bad9c158cbf9532e1ed1503b to your computer and use it in GitHub Desktop.
python app with loggin to rsyslog
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
""" | |
rsyslog config: /etc/rsyslog.d/my-sample.conf: | |
$template AppLogFormat, "%TIMESTAMP:::date-pgsql%%TIMESTAMP:27:32:date-rfc3339%(%syslogseverity-text%)%msg%\n" | |
if $app-name == 'my-sample-app' then -/var/log/my-sample-app/app.log;AppLogFormat | |
& ~ | |
""" | |
from __future__ import print_function | |
import argparse | |
import logging | |
import logging.handlers | |
APP_NAME = "my-sample-app" | |
def create_logger(app_name, log_level=logging.DEBUG, stdout=True, syslog=False, file=False): | |
""" | |
create logging object with logging to syslog, file and stdout | |
:param app_name app name | |
:param log_level logging log level | |
:param stdout log to stdout | |
:param syslog log to syslog | |
:param file log to file | |
:return: logging object | |
""" | |
# disable requests logging | |
#logging.getLogger("requests").setLevel(logging.ERROR) | |
#logging.getLogger("urllib3").setLevel(logging.ERROR) | |
# create logger | |
logger = logging.getLogger(app_name) | |
logger.setLevel(log_level) | |
# set log format to handlers | |
formatter = logging.Formatter('%(name)s - %(asctime)s - %(levelname)s - %(message)s') | |
if file: | |
# create file logger handler | |
fh = logging.FileHandler('my-sample-app.log') | |
fh.setLevel(log_level) | |
fh.setFormatter(formatter) | |
logger.addHandler(fh) | |
if syslog: | |
# create syslog logger handler | |
sh = logging.handlers.SysLogHandler(address='/dev/log') | |
sh.setLevel(log_level) | |
sf = logging.Formatter('%(name)s: %(message)s') | |
sh.setFormatter(sf) | |
logger.addHandler(sh) | |
if stdout: | |
# create stream logger handler | |
ch = logging.StreamHandler() | |
ch.setLevel(log_level) | |
ch.setFormatter(formatter) | |
logger.addHandler(ch) | |
return logger | |
if __name__ == "__main__": | |
print("BEGIN") | |
parser = argparse.ArgumentParser(description='sample app with logging') | |
parser.add_argument('-s', '--stdout', action='store_true', default=True, help='log to stdout') | |
parser.add_argument('-r', '--rsyslog', action='store_true', default=False, help='log to syslog') | |
parser.add_argument('-f', '--file', action='store_true', default=False, help='log file app.log') | |
args = parser.parse_args() | |
log = create_logger( | |
app_name=APP_NAME, | |
log_level=logging.DEBUG, | |
syslog=args.rsyslog, | |
stdout=args.stdout, | |
file=args.file) | |
log.debug("hello debug from my sample app") | |
log.debug("hello error from my sample app") | |
print("END") |
SumedhaJagtap
commented
Jun 7, 2019
via email
how your conf file matches that name with python code?
I changed the $app-name with another name which didn't executed.
…On Fri, Jun 7, 2019 at 1:17 PM Daniel Kraic ***@***.***> wrote:
Hi! $app-mame is part of rsyslog template
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/a1657f19bad9c158cbf9532e1ed1503b?email_source=notifications&email_token=AISEHCJ6LUIOQN5XQOYZBRLPZIHAXA5CNFSM4HVRRQA2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFTJHY#gistcomment-2937468>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AISEHCJSFLCANEINEDI74NTPZIHAXANCNFSM4HVRRQAQ>
.
Sorry, $app-mame is one of rsyslog properties - it is data item from log message: https://www.rsyslog.com/doc/v8-stable/configuration/properties.html
In python code syslog message is created using logging formatter. Part of this message is also app name. Message is then send to syslog (via /dev/log unix socket). Rsyslog service will read and parse received message and fill rsyslog properties from this message.
Ok, I got it.
Thank you very much for sharing this information with me.
…On Sun, Jun 9, 2019 at 9:56 PM Daniel Kraic ***@***.***> wrote:
Sorry, $app-mame is one of rsyslog properties - it is data item from log
message:
https://www.rsyslog.com/doc/v8-stable/configuration/properties.html
In python code syslog message is created using logging formatter. Part of
this message is also app name. Message is then send to syslog (via /dev/log
unix socket). Rsyslog service will read and parse received message and fill
rsyslog properties from this message.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/a1657f19bad9c158cbf9532e1ed1503b?email_source=notifications&email_token=AISEHCMEHCOCESZFQAX57WLPZUVLBA5CNFSM4HVRRQA2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFTMKS#gistcomment-2939049>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AISEHCJ2YBBJSDYEVEWPCZ3PZUVLBANCNFSM4HVRRQAQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment