Skip to content

Instantly share code, notes, and snippets.

@danielkraic
Last active February 28, 2024 12:35
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save danielkraic/a1657f19bad9c158cbf9532e1ed1503b to your computer and use it in GitHub Desktop.
Save danielkraic/a1657f19bad9c158cbf9532e1ed1503b to your computer and use it in GitHub Desktop.
python app with loggin to rsyslog
"""
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")
@pr11t
Copy link

pr11t commented Jan 11, 2018

Awesome!

@SumedhaJagtap
Copy link

You have used $app-name in /etc/rsyslog.d/my-sample.conf
How it actually work?
How does configuration file got to know that variable?
From where it gets it value?
Can you share any reference for writing such configuration files?

@danielkraic
Copy link
Author

danielkraic commented Jun 7, 2019

Hi! $app-mame is part of rsyslog template https://www.rsyslog.com/doc/v8-stable/configuration/templates.html
It is filled by python logging formatter.
In my example code, it is filled with value of app_name parameter in create_logger

@SumedhaJagtap
Copy link

SumedhaJagtap commented Jun 7, 2019 via email

@danielkraic
Copy link
Author

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.

@SumedhaJagtap
Copy link

SumedhaJagtap commented Jun 10, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment