{"id":2428,"date":"2014-06-25T18:27:50","date_gmt":"2014-06-25T16:27:50","guid":{"rendered":"https:\/\/supportex.net\/?p=2428"},"modified":"2019-05-10T15:44:28","modified_gmt":"2019-05-10T13:44:28","slug":"python-running-services-logging-rsyslog-fedora","status":"publish","type":"post","link":"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/","title":{"rendered":"Python: running services with logging to Rsyslog on Fedora"},"content":{"rendered":"<p>So, here is a task. We need to run some Python script as service with logging to Rsyslog on Fedora. As you may know, \u00a0Fedora starting from 15 uses <a href=\"http:\/\/fedoraproject.org\/wiki\/Features\/systemd\">systemd<\/a> which is a replacement for SysVinit and Upstart. With use of it to setup Python service with logging is quite simple. Let&#8217;s assume we have this logger function:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from config import DEBUG\ndef logger(message):\n    if DEBUG == 1:\n        print \"   \", message\n<\/pre>\n<p>Now we need to add systemd config for our service. Put if to \/lib\/systemd\/system\/web-py.service.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">[Unit]\nDescription=web-py\n\n[Service]\nExecStart=\/usr\/local\/bin\/web-py.py\nType=simple\nUser=web\nGroup=web\nRestart=always\nStandardOutput=syslog\nStandardError=syslog\nSyslogIdentifier=web-py\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n<p>Enable it:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">systemctl enable web-py.service<\/code><\/p>\n<p>Let&#8217;s set up Rsyslog to store logs from our service in a way we want:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">$template DynaFile,\"\/var\/log\/web\/%$YEAR%\/%$MONTH%\/%$DAY%\/%PROGRAMNAME%.log\"\nif $programname startswith 'web' then -?DynaFile\n&amp; ~<\/pre>\n<p>First line defines dynamic template <em>DynaFile<\/em>, the second one is quite self-explanatory. It says Rsyslog: if program name (<em>SyslogIdentifier<\/em> in our case) starts with &#8216;web&#8217;, then use <em>DynaFile<\/em> template. And the last (&#8220;&amp; ~&#8221;) forces Rsyslog not to try to find other matches for this message, otherwise it could be\u00a0also stored to <em>\/var\/log\/messages<\/em>, for instance, which is not what we are trying to get. It should be added before other facilities.<\/p>\n<p>Now start our service and restart Rsyslog:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">service web-py start\nservice rsyslog restart<\/pre>\n<p>Now after your service starts working you should have its logs in files like &#8216;\/var\/log\/web\/2011\/12\/07\/web_py.log&#8217;.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">[root@playground ~]#tail \/var\/log\/web\/2011\/12\/07\/web-py.log\n 2011-12-07T13:39:18+01:00 playground web-py[28635]: 2011-12-07 13:31:54.348987 - +starting web-py on 0.0.0.0:8189...<\/pre>\n<p>Working.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, here is a task. We need to run some Python script as service with logging to Rsyslog on Fedora. As you may know, \u00a0Fedora starting from 15 uses systemd which is a replacement for SysVinit and Upstart. With use of it to setup Python service with logging is quite simple. Let&#8217;s assume we have&hellip; <\/p>\n<div class=\"readmore-wrapper\"><a href=\"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/\" class=\"more-link\">Read <\/a><\/div>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[152],"tags":[],"class_list":["post-2428","post","type-post","status-publish","format-standard","hentry","category-linux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python: running services with logging to Rsyslog on Fedora &#8211; Supportex.NET blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: running services with logging to Rsyslog on Fedora &#8211; Supportex.NET blog\" \/>\n<meta property=\"og:description\" content=\"So, here is a task. We need to run some Python script as service with logging to Rsyslog on Fedora. As you may know, \u00a0Fedora starting from 15 uses systemd which is a replacement for SysVinit and Upstart. With use of it to setup Python service with logging is quite simple. Let&#8217;s assume we have&hellip; Read\" \/>\n<meta property=\"og:url\" content=\"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/\" \/>\n<meta property=\"og:site_name\" content=\"Supportex.NET blog\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-25T16:27:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-10T13:44:28+00:00\" \/>\n<meta name=\"author\" content=\"Oleksii Tykhonov\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Oleksii Tykhonov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/\"},\"author\":{\"name\":\"Oleksii Tykhonov\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"headline\":\"Python: running services with logging to Rsyslog on Fedora\",\"datePublished\":\"2014-06-25T16:27:50+00:00\",\"dateModified\":\"2019-05-10T13:44:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/\"},\"wordCount\":204,\"articleSection\":[\"linux\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/\",\"url\":\"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/\",\"name\":\"Python: running services with logging to Rsyslog on Fedora &#8211; Supportex.NET blog\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\"},\"datePublished\":\"2014-06-25T16:27:50+00:00\",\"dateModified\":\"2019-05-10T13:44:28+00:00\",\"author\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"breadcrumb\":{\"@id\":\"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/supportex.net\/blog\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python: running services with logging to Rsyslog on Fedora\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\",\"url\":\"https:\/\/supportex.net\/blog\/en\/\",\"name\":\"Supportex.NET blog\",\"description\":\"Server and network management company\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\",\"name\":\"Oleksii Tykhonov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"caption\":\"Oleksii Tykhonov\"},\"url\":\"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: running services with logging to Rsyslog on Fedora &#8211; Supportex.NET blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/","og_locale":"en_US","og_type":"article","og_title":"Python: running services with logging to Rsyslog on Fedora &#8211; Supportex.NET blog","og_description":"So, here is a task. We need to run some Python script as service with logging to Rsyslog on Fedora. As you may know, \u00a0Fedora starting from 15 uses systemd which is a replacement for SysVinit and Upstart. With use of it to setup Python service with logging is quite simple. Let&#8217;s assume we have&hellip; Read","og_url":"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/","og_site_name":"Supportex.NET blog","article_published_time":"2014-06-25T16:27:50+00:00","article_modified_time":"2019-05-10T13:44:28+00:00","author":"Oleksii Tykhonov","twitter_misc":{"Written by":"Oleksii Tykhonov","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/#article","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/"},"author":{"name":"Oleksii Tykhonov","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"headline":"Python: running services with logging to Rsyslog on Fedora","datePublished":"2014-06-25T16:27:50+00:00","dateModified":"2019-05-10T13:44:28+00:00","mainEntityOfPage":{"@id":"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/"},"wordCount":204,"articleSection":["linux"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/","url":"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/","name":"Python: running services with logging to Rsyslog on Fedora &#8211; Supportex.NET blog","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/en\/#website"},"datePublished":"2014-06-25T16:27:50+00:00","dateModified":"2019-05-10T13:44:28+00:00","author":{"@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"breadcrumb":{"@id":"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/supportex.net\/blog\/2014\/06\/python-running-services-logging-rsyslog-fedora\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/supportex.net\/blog\/en\/"},{"@type":"ListItem","position":2,"name":"Python: running services with logging to Rsyslog on Fedora"}]},{"@type":"WebSite","@id":"https:\/\/supportex.net\/blog\/en\/#website","url":"https:\/\/supportex.net\/blog\/en\/","name":"Supportex.NET blog","description":"Server and network management company","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251","name":"Oleksii Tykhonov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","caption":"Oleksii Tykhonov"},"url":"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/"}]}},"_links":{"self":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/comments?post=2428"}],"version-history":[{"count":2,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2428\/revisions"}],"predecessor-version":[{"id":3129,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2428\/revisions\/3129"}],"wp:attachment":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/media?parent=2428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/categories?post=2428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/tags?post=2428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}