├── .coveragerc ├── .github └── workflows │ └── python-package.yml ├── .gitignore ├── AUTHORS ├── CHANGELOG ├── CONTRIBUTING ├── INSTALL ├── LICENSE ├── MANIFEST.in ├── README.rst ├── demo └── simple.py ├── docs ├── Makefile ├── build │ └── empty ├── requirements.txt └── source │ ├── _static │ └── empty │ ├── _templates │ └── empty │ ├── api.rst │ ├── conf.py │ ├── index.rst │ ├── quickstart.rst │ └── rst_guide.rst ├── setup.cfg ├── setup.py ├── systemdlogging ├── __init__.py ├── backend_ctyped.py └── toolbox.py ├── tests └── test_module.py └── tox.ini /.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | source = systemdlogging/ 3 | 4 | -------------------------------------------------------------------------------- /.github/workflows/python-package.yml: -------------------------------------------------------------------------------- 1 | # This workflow will install Python dependencies, run tests and lint with a variety of Python versions 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions 3 | 4 | name: Python package 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | strategy: 17 | fail-fast: false 18 | matrix: 19 | python-version: [3.6, 3.7, 3.8, 3.9, "3.10"] 20 | 21 | steps: 22 | - uses: actions/checkout@v2 23 | - name: Set up Python ${{ matrix.python-version }} 24 | uses: actions/setup-python@v2 25 | with: 26 | python-version: ${{ matrix.python-version }} 27 | - name: Install deps 28 | run: | 29 | python -m pip install pytest coverage coveralls 30 | - name: Run tests 31 | env: 32 | GITHUB_TOKEN: ${{ secrets.github_token }} 33 | run: | 34 | coverage run --source=systemdlogging setup.py test 35 | coveralls --service=github 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | .pydevproject 3 | .idea 4 | .tox 5 | __pycache__ 6 | *.pyc 7 | *.pyo 8 | *.egg-info 9 | docs/_build/ 10 | 11 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | systemd-logging authors 2 | ======================= 3 | 4 | Created by Igor `idle sign` Starikov. 5 | 6 | 7 | Contributors 8 | ------------ 9 | 10 | Here could be your name. 11 | 12 | 13 | 14 | Translators 15 | ----------- 16 | 17 | Here could be your name. 18 | 19 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | systemd-logging changelog 2 | ========================= 3 | 4 | 5 | v1.0.1 [2022-01-28] 6 | ------------------- 7 | * Prevent segfaults for values having format placeholders like %s (see #6). 8 | 9 | 10 | v1.0.0 [2021-07-14] 11 | ------------------- 12 | * Celebrating 1.0.0. 13 | * CtypedException is now hidden to improve plugability (see #5). 14 | 15 | 16 | v0.3.1 [2021-01-25] 17 | ------------------- 18 | * Proper % handling in messages (see #3). 19 | 20 | 21 | v0.3.0 [2020-01-28] 22 | ------------------- 23 | + init_systemd_logging() now tries to detect systemd and returns boolean. 24 | 25 | 26 | v0.2.0 [2019-10-15] 27 | ------------------- 28 | + Added an option to set SYSLOG_IDENTIFIER. 29 | * Removed PROCESS_ID field in favor of _PID. 30 | 31 | 32 | v0.1.0 [2019-10-14] 33 | ------------------- 34 | + Basic functionality. -------------------------------------------------------------------------------- /CONTRIBUTING: -------------------------------------------------------------------------------- 1 | systemd-logging contributing 2 | ============================ 3 | 4 | 5 | Submit issues 6 | ------------- 7 | 8 | If you spotted something weird in application behavior or want to propose a feature you are welcome. 9 | 10 | 11 | Write code 12 | ---------- 13 | If you are eager to participate in application development and to work on an existing issue (whether it should 14 | be a bugfix or a feature implementation), fork, write code, and make a pull request right from the forked project page. 15 | 16 | 17 | Spread the word 18 | --------------- 19 | 20 | If you have some tips and tricks or any other words that you think might be of interest for the others — publish it 21 | wherever you find convenient. 22 | 23 | 24 | See also: https://github.com/idlesign/systemd-logging 25 | 26 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | systemd-logging installation 2 | ============================ 3 | 4 | 5 | Python ``pip`` package is required to install ``systemd-logging``. 6 | 7 | 8 | From sources 9 | ------------ 10 | 11 | Use the following command line to install ``systemd-logging`` from sources directory (containing setup.py): 12 | 13 | pip install . 14 | 15 | or 16 | 17 | python setup.py install 18 | 19 | 20 | From PyPI 21 | --------- 22 | 23 | Alternatively you can install ``systemd-logging`` from PyPI: 24 | 25 | pip install systemd-logging 26 | 27 | 28 | Use `-U` flag for upgrade: 29 | 30 | pip install -U systemd-logging 31 | 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019-2022, Igor `idle sign` Starikov 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of the systemd-logging nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include AUTHORS 2 | include CHANGELOG 3 | include INSTALL 4 | include LICENSE 5 | include README.rst 6 | 7 | include docs/Makefile 8 | recursive-include docs *.rst 9 | recursive-include docs *.py 10 | recursive-include tests * 11 | 12 | recursive-exclude * __pycache__ 13 | recursive-exclude * *.py[co] 14 | recursive-exclude * empty 15 | 16 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | systemd-logging 2 | =============== 3 | https://github.com/idlesign/systemd-logging 4 | 5 | |release| |lic| |coverage| 6 | 7 | .. |release| image:: https://img.shields.io/pypi/v/systemd-logging.svg 8 | :target: https://pypi.python.org/pypi/systemd-logging 9 | 10 | .. |lic| image:: https://img.shields.io/pypi/l/systemd-logging.svg 11 | :target: https://pypi.python.org/pypi/systemd-logging 12 | 13 | .. |coverage| image:: https://img.shields.io/coveralls/idlesign/systemd-logging/master.svg 14 | :target: https://coveralls.io/r/idlesign/systemd-logging 15 | 16 | 17 | Description 18 | ----------- 19 | 20 | *Simplifies logging for systemd* 21 | 22 | **Requires Python 3.6+** 23 | 24 | * No need to compile (pure Python), uses ``libsystemd.so``. 25 | * Simplified configuration. 26 | * Just logging. Nothing more. 27 | 28 | 29 | Usage 30 | ----- 31 | 32 | .. code-block:: python 33 | 34 | import logging 35 | 36 | from systemdlogging.toolbox import init_systemd_logging 37 | 38 | # This one line in most cases would be enough. 39 | # By default it attaches systemd logging handler to a root Python logger. 40 | init_systemd_logging() # Returns True if initialization went fine. 41 | 42 | # Now you can use logging as usual. 43 | logger = logging.getLogger(__name__) 44 | logger.setLevel(logging.DEBUG) 45 | 46 | logger.debug('My debug message') 47 | 48 | try: 49 | raise ValueError('Log me please') 50 | 51 | except ValueError: 52 | # Additional context can be passed in extra.context. 53 | logger.exception('Something terrible just happened', extra={ 54 | 'message_id': True, # Generate message ID automatically. 55 | 'context': { 56 | 'FIELD1': 'one', 57 | 'FIELD2': 'two', 58 | } 59 | }, stack_info=True) 60 | 61 | 62 | Read the docs to find out more. 63 | 64 | 65 | Documentation 66 | ------------- 67 | 68 | https://systemd-logging.readthedocs.org/ 69 | 70 | Debug runs 71 | ~~~~~~~~~~ 72 | 73 | 1. Run your script with: 74 | 75 | .. code-block:: 76 | 77 | $ sudo systemd-run -u debugme -t /usr/bin/python /home/my/testme.py 78 | 79 | 2. Watch its journal with: 80 | 81 | .. code-block:: 82 | 83 | $ journalctl -u debugme -f -o verbose 84 | -------------------------------------------------------------------------------- /demo/simple.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python3 2 | """ 3 | Demo, showing `systemd-logging` in action. 4 | 5 | Run this script as a transient systemd service and watch for the records generated by our service: 6 | 7 | $ sudo systemd-run --unit logtest python3 $PWD/simple.py && journalctl -fu logtest.service 8 | 9 | Other commands: 10 | 11 | * Show only error records for service: 12 | $ journalctl -fu logtest.service -o verbose -p "err" 13 | 14 | * Filter message by `SYSLOG_IDENTIFIER`: 15 | $ journalctl SYSLOG_IDENTIFIER=logtest 16 | 17 | * Show 'Take that' events by message ID: 18 | $ journalctl MESSAGE_ID=021fd3defec83b0cad1f544e39716517 19 | 20 | """ 21 | import logging 22 | 23 | from systemdlogging.toolbox import init_systemd_logging 24 | 25 | if not init_systemd_logging(syslog_id='logtest'): 26 | raise ProcessLookupError("It seems this process is not run under systemd.") 27 | 28 | logger = logging.getLogger(__name__) 29 | logger.setLevel(logging.DEBUG) 30 | 31 | 32 | # Now we're ready to issue messages. 33 | def main(): 34 | 35 | def raiser(msg): 36 | raise ValueError(msg) 37 | 38 | logger.debug('My debug 1') 39 | logger.debug('My debug 2') 40 | 41 | for _ in range(2): 42 | logger.info('Take that', extra={'message_id': True}) 43 | 44 | try: 45 | raiser('Catch me please') 46 | 47 | except ValueError: 48 | 49 | logger.exception('Something nasty has happened', extra={ 50 | 'context': { 51 | 'FIELD_1': 'Value one', 52 | 'FIELD_2': 'Value two', 53 | } 54 | }, stack_info=True) 55 | 56 | 57 | if __name__ == '__main__': 58 | main() 59 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = build 9 | 10 | # Internal variables. 11 | PAPEROPT_a4 = -D latex_paper_size=a4 12 | PAPEROPT_letter = -D latex_paper_size=letter 13 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source 14 | 15 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest 16 | 17 | help: 18 | @echo "Please use \`make ' where is one of" 19 | @echo " html to make standalone HTML files" 20 | @echo " dirhtml to make HTML files named index.html in directories" 21 | @echo " singlehtml to make a single large HTML file" 22 | @echo " pickle to make pickle files" 23 | @echo " json to make JSON files" 24 | @echo " htmlhelp to make HTML files and a HTML help project" 25 | @echo " qthelp to make HTML files and a qthelp project" 26 | @echo " devhelp to make HTML files and a Devhelp project" 27 | @echo " epub to make an epub" 28 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 29 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 30 | @echo " text to make text files" 31 | @echo " man to make manual pages" 32 | @echo " changes to make an overview of all changed/added/deprecated items" 33 | @echo " linkcheck to check all external links for integrity" 34 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 35 | 36 | clean: 37 | -rm -rf $(BUILDDIR)/* 38 | 39 | html: 40 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 41 | @echo 42 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 43 | 44 | dirhtml: 45 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 46 | @echo 47 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 48 | 49 | singlehtml: 50 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 51 | @echo 52 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 53 | 54 | pickle: 55 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 56 | @echo 57 | @echo "Build finished; now you can process the pickle files." 58 | 59 | json: 60 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 61 | @echo 62 | @echo "Build finished; now you can process the JSON files." 63 | 64 | htmlhelp: 65 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 66 | @echo 67 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 68 | ".hhp project file in $(BUILDDIR)/htmlhelp." 69 | 70 | qthelp: 71 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 72 | @echo 73 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 74 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 75 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/systemd-logging.qhcp" 76 | @echo "To view the help file:" 77 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/systemd-logging.qhc" 78 | 79 | devhelp: 80 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 81 | @echo 82 | @echo "Build finished." 83 | @echo "To view the help file:" 84 | @echo "# mkdir -p $$HOME/.local/share/devhelp/systemd-logging" 85 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/systemd-logging" 86 | @echo "# devhelp" 87 | 88 | epub: 89 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 90 | @echo 91 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 92 | 93 | latex: 94 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 95 | @echo 96 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 97 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 98 | "(use \`make latexpdf' here to do that automatically)." 99 | 100 | latexpdf: 101 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 102 | @echo "Running LaTeX files through pdflatex..." 103 | make -C $(BUILDDIR)/latex all-pdf 104 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 105 | 106 | text: 107 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 108 | @echo 109 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 110 | 111 | man: 112 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 113 | @echo 114 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 115 | 116 | changes: 117 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 118 | @echo 119 | @echo "The overview file is in $(BUILDDIR)/changes." 120 | 121 | linkcheck: 122 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 123 | @echo 124 | @echo "Link check complete; look for any errors in the above output " \ 125 | "or in $(BUILDDIR)/linkcheck/output.txt." 126 | 127 | doctest: 128 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 129 | @echo "Testing of doctests in the sources finished, look at the " \ 130 | "results in $(BUILDDIR)/doctest/output.txt." 131 | 132 | -------------------------------------------------------------------------------- /docs/build/empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/idlesign/systemd-logging/a3ad462c189aa059a3d8fa0ef63bb79b9c79b501/docs/build/empty -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | ctyped>=0.7.1 2 | -------------------------------------------------------------------------------- /docs/source/_static/empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/idlesign/systemd-logging/a3ad462c189aa059a3d8fa0ef63bb79b9c79b501/docs/source/_static/empty -------------------------------------------------------------------------------- /docs/source/_templates/empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/idlesign/systemd-logging/a3ad462c189aa059a3d8fa0ef63bb79b9c79b501/docs/source/_templates/empty -------------------------------------------------------------------------------- /docs/source/api.rst: -------------------------------------------------------------------------------- 1 | API 2 | === 3 | 4 | .. automodule:: systemdlogging.toolbox 5 | :members: 6 | -------------------------------------------------------------------------------- /docs/source/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # systemd-logging documentation build configuration file. 4 | # 5 | # This file is execfile()d with the current directory set to its containing dir. 6 | # 7 | # Note that not all possible configuration values are present in this 8 | # autogenerated file. 9 | # 10 | # All configuration values have a default; values that are commented out 11 | # serve to show the default. 12 | 13 | import sys, os 14 | 15 | # If extensions (or modules to document with autodoc) are in another directory, 16 | # add these directories to sys.path here. If the directory is relative to the 17 | # documentation root, use os.path.abspath to make it absolute, like shown here. 18 | sys.path.insert(0, os.path.abspath('../../')) 19 | from systemdlogging import VERSION 20 | 21 | # -- Mocking ------------------------------------------------------------------ 22 | 23 | # This is used to mock certain modules. 24 | # It helps to build docs in environments where those modules are not available. 25 | # E.g. it could be useful for http://readthedocs.org/ 26 | MODULES_TO_MOCK = [ 27 | 'ctyped', 28 | 'ctyped.toolbox', 29 | 'ctyped.exceptions', 30 | ] 31 | 32 | 33 | if MODULES_TO_MOCK: 34 | 35 | class ModuleMock(object): 36 | 37 | __all__ = [] 38 | 39 | def __init__(self, *args, **kwargs): 40 | pass 41 | 42 | def __call__(self, *args, **kwargs): 43 | return ModuleMock() 44 | 45 | def __iter__(self): 46 | return iter([]) 47 | 48 | @classmethod 49 | def __getattr__(cls, name): 50 | if name in ('__file__', '__path__'): 51 | return '/dev/null' 52 | elif name.upper() != name and name[0] == name[0].upper(): 53 | # Mock classes. 54 | MockType = type(name, (ModuleMock,), {}) 55 | MockType.__module__ = __name__ 56 | return MockType 57 | return ModuleMock() 58 | 59 | for mod_name in MODULES_TO_MOCK: 60 | sys.modules[mod_name] = ModuleMock() 61 | 62 | 63 | # -- General configuration ----------------------------------------------------- 64 | 65 | # If your documentation needs a minimal Sphinx version, state it here. 66 | #needs_sphinx = '1.0' 67 | 68 | # Add any Sphinx extension module names here, as strings. They can be extensions 69 | # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 70 | extensions = ['sphinx.ext.autodoc'] 71 | 72 | # Instruct autoclass directive to document both class and __init__ docstrings. 73 | autoclass_content = 'both' 74 | 75 | # Add any paths that contain templates here, relative to this directory. 76 | templates_path = ['_templates'] 77 | 78 | # The suffix of source filenames. 79 | source_suffix = '.rst' 80 | 81 | # The encoding of source files. 82 | #source_encoding = 'utf-8-sig' 83 | 84 | # The master toctree document. 85 | master_doc = 'index' 86 | 87 | # General information about the project. 88 | project = u'systemd-logging' 89 | copyright = u'2019-2022, Igor `idle sign` Starikov' 90 | 91 | # The version info for the project you're documenting, acts as replacement for 92 | # |version| and |release|, also used in various other places throughout the 93 | # built documents. 94 | # 95 | # The short X.Y version. 96 | version = '.'.join(map(str, VERSION)) 97 | # The full version, including alpha/beta/rc tags. 98 | release = '.'.join(map(str, VERSION)) 99 | 100 | # The language for content autogenerated by Sphinx. Refer to documentation 101 | # for a list of supported languages. 102 | #language = None 103 | 104 | # There are two options for replacing |today|: either, you set today to some 105 | # non-false value, then it is used: 106 | #today = '' 107 | # Else, today_fmt is used as the format for a strftime call. 108 | #today_fmt = '%B %d, %Y' 109 | 110 | # List of patterns, relative to source directory, that match files and 111 | # directories to ignore when looking for source files. 112 | exclude_patterns = ['rst_guide.rst'] 113 | 114 | # The reST default role (used for this markup: `text`) to use for all documents. 115 | #default_role = None 116 | 117 | # If true, '()' will be appended to :func: etc. cross-reference text. 118 | #add_function_parentheses = True 119 | 120 | # If true, the current module name will be prepended to all description 121 | # unit titles (such as .. function::). 122 | #add_module_names = True 123 | 124 | # If true, sectionauthor and moduleauthor directives will be shown in the 125 | # output. They are ignored by default. 126 | #show_authors = False 127 | 128 | # The name of the Pygments (syntax highlighting) style to use. 129 | pygments_style = 'sphinx' 130 | 131 | # A list of ignored prefixes for module index sorting. 132 | #modindex_common_prefix = [] 133 | 134 | 135 | # -- Options for HTML output --------------------------------------------------- 136 | 137 | # The theme to use for HTML and HTML Help pages. See the documentation for 138 | # a list of builtin themes. 139 | html_theme = 'default' 140 | 141 | # Theme options are theme-specific and customize the look and feel of a theme 142 | # further. For a list of options available for each theme, see the 143 | # documentation. 144 | #html_theme_options = {} 145 | 146 | # Add any paths that contain custom themes here, relative to this directory. 147 | #html_theme_path = [] 148 | 149 | # The name for this set of Sphinx documents. If None, it defaults to 150 | # " v documentation". 151 | #html_title = None 152 | 153 | # A shorter title for the navigation bar. Default is the same as html_title. 154 | #html_short_title = None 155 | 156 | # The name of an image file (relative to this directory) to place at the top 157 | # of the sidebar. 158 | #html_logo = None 159 | 160 | # The name of an image file (within the static path) to use as favicon of the 161 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 162 | # pixels large. 163 | #html_favicon = None 164 | 165 | # Add any paths that contain custom static files (such as style sheets) here, 166 | # relative to this directory. They are copied after the builtin static files, 167 | # so a file named "default.css" will overwrite the builtin "default.css". 168 | html_static_path = ['_static'] 169 | 170 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 171 | # using the given strftime format. 172 | #html_last_updated_fmt = '%b %d, %Y' 173 | 174 | # If true, SmartyPants will be used to convert quotes and dashes to 175 | # typographically correct entities. 176 | #html_use_smartypants = True 177 | 178 | # Custom sidebar templates, maps document names to template names. 179 | #html_sidebars = {} 180 | 181 | # Additional templates that should be rendered to pages, maps page names to 182 | # template names. 183 | #html_additional_pages = {} 184 | 185 | # If false, no module index is generated. 186 | #html_domain_indices = True 187 | 188 | # If false, no index is generated. 189 | #html_use_index = True 190 | 191 | # If true, the index is split into individual pages for each letter. 192 | #html_split_index = False 193 | 194 | # If true, links to the reST sources are added to the pages. 195 | #html_show_sourcelink = True 196 | 197 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 198 | #html_show_sphinx = True 199 | 200 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 201 | #html_show_copyright = True 202 | 203 | # If true, an OpenSearch description file will be output, and all pages will 204 | # contain a tag referring to it. The value of this option must be the 205 | # base URL from which the finished HTML is served. 206 | #html_use_opensearch = '' 207 | 208 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 209 | #html_file_suffix = None 210 | 211 | # Output file base name for HTML help builder. 212 | htmlhelp_basename = 'systemd-loggingdoc' 213 | 214 | 215 | # -- Options for LaTeX output -------------------------------------------------- 216 | 217 | # The paper size ('letter' or 'a4'). 218 | #latex_paper_size = 'letter' 219 | 220 | # The font size ('10pt', '11pt' or '12pt'). 221 | #latex_font_size = '10pt' 222 | 223 | # Grouping the document tree into LaTeX files. List of tuples 224 | # (source start file, target name, title, author, documentclass [howto/manual]). 225 | latex_documents = [ 226 | ('index', 'systemd-logging.tex', u'systemd-logging Documentation', 227 | u'Igor `idle sign` Starikov', 'manual'), 228 | ] 229 | 230 | # The name of an image file (relative to this directory) to place at the top of 231 | # the title page. 232 | #latex_logo = None 233 | 234 | # For "manual" documents, if this is true, then toplevel headings are parts, 235 | # not chapters. 236 | #latex_use_parts = False 237 | 238 | # If true, show page references after internal links. 239 | #latex_show_pagerefs = False 240 | 241 | # If true, show URL addresses after external links. 242 | #latex_show_urls = False 243 | 244 | # Additional stuff for the LaTeX preamble. 245 | #latex_preamble = '' 246 | 247 | # Documents to append as an appendix to all manuals. 248 | #latex_appendices = [] 249 | 250 | # If false, no module index is generated. 251 | #latex_domain_indices = True 252 | 253 | 254 | # -- Options for manual page output -------------------------------------------- 255 | 256 | # One entry per manual page. List of tuples 257 | # (source start file, name, description, authors, manual section). 258 | man_pages = [ 259 | ('index', 'systemd-logging', u'systemd-logging Documentation', 260 | [u'Igor `idle sign` Starikov'], 1) 261 | ] 262 | 263 | -------------------------------------------------------------------------------- /docs/source/index.rst: -------------------------------------------------------------------------------- 1 | systemd-logging documentation 2 | ============================= 3 | https://github.com/idlesign/systemd-logging 4 | 5 | 6 | 7 | Description 8 | ----------- 9 | 10 | *Simplifies logging for systemd* 11 | 12 | * No need to compile (pure Python), uses ``libsystemd.so``. 13 | * Simplified configuration. 14 | * Just logging. Nothing more. 15 | 16 | 17 | 18 | Requirements 19 | ------------ 20 | 21 | 1. Python 3.6+ 22 | 23 | 24 | 25 | Table of Contents 26 | ----------------- 27 | 28 | .. toctree:: 29 | :maxdepth: 3 30 | 31 | quickstart 32 | api 33 | -------------------------------------------------------------------------------- /docs/source/quickstart.rst: -------------------------------------------------------------------------------- 1 | Quickstart 2 | ========== 3 | 4 | 5 | .. code-block:: python 6 | 7 | import logging 8 | 9 | from systemdlogging.toolbox import init_systemd_logging 10 | 11 | # This one line in most cases would be enough. 12 | # By default it attaches systemd logging handler to a root Python logger. 13 | init_systemd_logging() # Returns True if initialization went fine. 14 | 15 | # Now you can use logging as usual. 16 | logger = logging.getLogger(__name__) 17 | logger.setLevel(logging.DEBUG) 18 | 19 | logger.debug('My debug message') 20 | 21 | try: 22 | raise ValueError('Log me please') 23 | 24 | except ValueError: 25 | # Additional context can be passed in extra.context. 26 | logger.exception('Something terrible just happened', extra={ 27 | 'message_id': True, # Generate message ID automatically. 28 | 'context': { 29 | 'FIELD1': 'one', 30 | 'FIELD2': 'two', 31 | } 32 | }, stack_info=True) 33 | 34 | -------------------------------------------------------------------------------- /docs/source/rst_guide.rst: -------------------------------------------------------------------------------- 1 | RST Quick guide 2 | =============== 3 | 4 | Online reStructuredText editor - http://rst.ninjs.org/ 5 | 6 | 7 | Main heading 8 | ============ 9 | 10 | 11 | Secondary heading 12 | ----------------- 13 | 14 | Minor heading 15 | ~~~~~~~~~~~~~ 16 | 17 | 18 | Typography 19 | ---------- 20 | 21 | **Bold** 22 | 23 | `Italic` 24 | 25 | ``Accent`` 26 | 27 | 28 | 29 | Blocks 30 | ------ 31 | 32 | Double colon to consider the following paragraphs preformatted:: 33 | 34 | This text is preformated. Can be used for code samples. 35 | 36 | 37 | .. code-block:: python 38 | 39 | # code-block accepts language name to highlight code 40 | # E.g.: python, html 41 | import this 42 | 43 | 44 | .. note:: 45 | 46 | This text will be rendered as a note block (usually green). 47 | 48 | 49 | .. warning:: 50 | 51 | This text will be rendered as a warning block (usually red). 52 | 53 | 54 | 55 | Lists 56 | ----- 57 | 58 | 1. Ordered item 1. 59 | 60 | Indent paragraph to make in belong to the above list item. 61 | 62 | 2. Ordered item 2. 63 | 64 | 65 | + Unordered item 1. 66 | + Unordered item . 67 | 68 | 69 | 70 | Links 71 | ----- 72 | 73 | :ref:`Documentation inner link label ` 74 | 75 | .. _some-marker: 76 | 77 | 78 | `Outer link label `_ 79 | 80 | Inline URLs are converted to links automatically: http://github.com/idlesign/makeapp/ 81 | 82 | 83 | Images 84 | ------ 85 | 86 | .. image:: path_to_image/image.png 87 | 88 | 89 | Automation 90 | ---------- 91 | 92 | http://sphinx-doc.org/ext/autodoc.html 93 | 94 | .. automodule:: my_module 95 | :members: 96 | 97 | .. autoclass:: my_module.MyClass 98 | :members: do_this, do_that 99 | :inherited-members: 100 | :undoc-members: 101 | :private-members: 102 | :special-members: 103 | :show-inheritance: 104 | 105 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [aliases] 2 | release = clean --all sdist bdist_wheel upload 3 | test = pytest 4 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import io 2 | import os 3 | import re 4 | 5 | from setuptools import setup, find_packages 6 | 7 | import sys 8 | 9 | PATH_BASE = os.path.dirname(__file__) 10 | 11 | 12 | def read_file(fpath): 13 | """Reads a file within package directories.""" 14 | with io.open(os.path.join(PATH_BASE, fpath)) as f: 15 | return f.read() 16 | 17 | 18 | def get_version(): 19 | """Returns version number, without module import (which can lead to ImportError 20 | if some dependencies are unavailable before install.""" 21 | contents = read_file(os.path.join('systemdlogging', '__init__.py')) 22 | version = re.search('VERSION = \(([^)]+)\)', contents) 23 | version = version.group(1).replace(', ', '.').strip() 24 | return version 25 | 26 | 27 | setup( 28 | name='systemd-logging', 29 | version=get_version(), 30 | url='https://github.com/idlesign/systemd-logging', 31 | 32 | description='Simplifies logging for systemd', 33 | long_description=read_file('README.rst'), 34 | license='BSD 3-Clause License', 35 | 36 | author='Igor `idle sign` Starikov', 37 | author_email='idlesign@yandex.ru', 38 | 39 | packages=find_packages(), 40 | include_package_data=True, 41 | zip_safe=False, 42 | 43 | install_requires=[ 44 | 'ctyped>=0.7.1', 45 | ], 46 | setup_requires=[] + (['pytest-runner'] if 'test' in sys.argv else []) + [], 47 | 48 | entry_points={ 49 | }, 50 | 51 | test_suite='tests', 52 | 53 | tests_require=['pytest'], 54 | 55 | classifiers=[ 56 | # As in https://pypi.python.org/pypi?:action=list_classifiers 57 | 'Development Status :: 5 - Production/Stable', 58 | 'Operating System :: OS Independent', 59 | 'Programming Language :: Python', 60 | 'Programming Language :: Python :: 3', 61 | 'Programming Language :: Python :: 3.6', 62 | 'Programming Language :: Python :: 3.7', 63 | 'Programming Language :: Python :: 3.8', 64 | 'Programming Language :: Python :: 3.9', 65 | 'Programming Language :: Python :: 3.10', 66 | 'License :: OSI Approved :: BSD License' 67 | ], 68 | ) 69 | -------------------------------------------------------------------------------- /systemdlogging/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | VERSION = (1, 0, 1) 4 | """Application version number tuple.""" 5 | 6 | VERSION_STR = '.'.join(map(str, VERSION)) 7 | """Application version number string.""" -------------------------------------------------------------------------------- /systemdlogging/backend_ctyped.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | from ctyped.exceptions import CtypedException 4 | from ctyped.toolbox import Library 5 | 6 | LOGGER = logging.getLogger(__name__) 7 | 8 | 9 | def send_stub(*fmt): # pragma: nocover 10 | """Dummy send function to be used when the real one 11 | is not available (e.g. no libsystemd found). 12 | 13 | """ 14 | return None 15 | 16 | 17 | try: 18 | lib = Library('libsystemd.so', prefix='sd_journal_') 19 | 20 | except CtypedException: # pragma: nocover 21 | 22 | send = send_stub 23 | 24 | LOGGER.warning("systemd library is not available and won't be used for logging") 25 | 26 | else: 27 | 28 | @lib.function() 29 | def send(*fmt) -> int: # pragma: nocover 30 | ... 31 | 32 | 33 | lib.bind_types() 34 | -------------------------------------------------------------------------------- /systemdlogging/toolbox.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import os 3 | import syslog 4 | import uuid 5 | from typing import Optional 6 | 7 | from .backend_ctyped import send, send_stub 8 | 9 | PRIORITY_MAP = { 10 | logging.CRITICAL: syslog.LOG_CRIT, 11 | logging.ERROR: syslog.LOG_ERR, 12 | logging.WARNING: syslog.LOG_WARNING, 13 | logging.INFO: syslog.LOG_INFO, 14 | logging.DEBUG: syslog.LOG_DEBUG, 15 | } 16 | 17 | 18 | def log_message(*, level: int, msg: str, context: Optional[dict] = None) -> bool: 19 | """Sends a message to systemd. 20 | 21 | .. warning:: Low level function. You may want to 22 | use more convenient `init_systemd_logging()` or `SystemdHandler` and `SystemdFormatter`. 23 | 24 | Natively supported fields: 25 | http://0pointer.de/public/systemd-man/systemd.journal-fields.html 26 | 27 | :param level: Logging level. 28 | :param msg: Message text to send. 29 | :param context: Additional context to send within message. 30 | 31 | """ 32 | args = { 33 | 'MESSAGE': msg, 34 | 'PRIORITY': PRIORITY_MAP.get(level, level), 35 | } 36 | 37 | args.update(context or {}) 38 | 39 | return send( 40 | # escape % in values to prevent segfaults 41 | # while trying to format things like %s, %d, etc. 42 | *[f'{key}={val}'.replace('%', '%%').encode() for key, val in args.items()], 43 | None 44 | ) == 0 45 | 46 | 47 | class SystemdHandler(logging.Handler): 48 | """Allows passing log messages to systemd. 49 | 50 | Additional information may be passed in ``extra`` dictionary: 51 | 52 | * context - Dictionary with additional information to attach to a message. 53 | * message_id - Message ID (usually UUID to assign to the message). 54 | If ``True`` ID will be generated automatically 55 | 56 | .. code-block: python 57 | 58 | logger.exception('This is my exceptional log entry.', extra={ 59 | 'message_id': True, 60 | 'context': { 61 | 'MY_FIELD': 'My value', 62 | } 63 | }, stack_info=True) 64 | 65 | Fills in the following fields automatically: 66 | 67 | * CODE_FILE - Filename 68 | * CODE_LINE - Code line number 69 | * CODE_FUNC - Code function name 70 | * CODE_MODULE - Python module name 71 | * LOGGER - Logger name 72 | * THREAD_ID - Thread ID if any 73 | * THREAD_NAME - Thread name if any 74 | * PROCESS_NAME - Process name if any 75 | * PRIORITY - Priority based on logging level 76 | 77 | Optionally fills in: 78 | * TRACEBACK - Traceback information if available 79 | * STACK - Stacktrace information if available 80 | * MESSAGE_ID - Message ID (if context.message_id=True) 81 | 82 | """ 83 | 84 | syslog_id: str = '' 85 | """SYSLOG_IDENTIFIER to add to message. If not set command name is used.""" 86 | 87 | def emit(self, record: logging.LogRecord): 88 | 89 | context = { 90 | # Generic 91 | 'CODE_FILE': record.pathname, 92 | 'CODE_LINE': record.lineno, 93 | 'CODE_FUNC': record.funcName, 94 | # Custom 95 | 'CODE_MODULE': record.module, 96 | 'LOGGER': record.name, 97 | 'THREAD_ID': record.thread, 98 | 'THREAD_NAME': record.threadName, 99 | 'PROCESS_NAME': record.processName, 100 | } 101 | 102 | try: 103 | exc_info = record.exc_info 104 | if exc_info: 105 | context['TRACEBACK'] = self.formatter.formatException(exc_info) 106 | 107 | stack_info = record.stack_info 108 | if stack_info: 109 | context['STACK'] = self.formatter.formatStack(stack_info) 110 | 111 | syslog_id = self.syslog_id 112 | if syslog_id: 113 | context['SYSLOG_IDENTIFIER'] = syslog_id 114 | 115 | msg = self.format(record) 116 | 117 | message_id = getattr(record, 'message_id', None) 118 | 119 | if message_id: 120 | 121 | if message_id is True: 122 | # Generate message ID automatically. 123 | message_id = uuid.uuid3(uuid.NAMESPACE_OID, '|'.join(str(val) for val in ( 124 | msg, record.levelno, record.pathname, record.funcName 125 | ))).hex 126 | 127 | context['MESSAGE_ID'] = message_id 128 | 129 | context_ = getattr(record, 'context', {}) 130 | context.update(context_) 131 | 132 | log_message(level=record.levelno, msg=msg, context=context) 133 | 134 | except Exception: # pragma: nocover 135 | self.handleError(record) 136 | 137 | 138 | class SystemdFormatter(logging.Formatter): 139 | """Formatter for systemd.""" 140 | 141 | def format(self, record): 142 | # This one is just like its parent just doesn't 143 | # append traceback etc. to the message. 144 | record.message = record.getMessage() 145 | return self.formatMessage(record) 146 | 147 | 148 | def check_for_systemd() -> bool: 149 | """Checks whether systemd library is available and 150 | the current process is run under systemd (and thus its output 151 | is connected to journald). 152 | 153 | """ 154 | return (send is not send_stub) and bool(os.environ.get('INVOCATION_ID')) # Works with systemd v232+ 155 | 156 | 157 | def init_systemd_logging(*, logger: Optional[logging.Logger] = None, syslog_id: str = '') -> bool: 158 | """Initializes logging to send log messages to systemd. 159 | 160 | Returns boolean indicating initialization went fine (see also `check_for_systemd()`). 161 | 162 | If it wasn't one may either to fallback to another logging handler 163 | or leave it as it is possibly sacrificing some log context. 164 | 165 | :param logger: Logger to attach systemd logging handler to. 166 | If not set handler is attached to a root logger. 167 | 168 | :param syslog_id: Value to be used in SYSLOG_IDENTIFIER message field. 169 | 170 | """ 171 | systemd_ok = check_for_systemd() 172 | 173 | if systemd_ok: 174 | handler = SystemdHandler() 175 | handler.syslog_id = syslog_id 176 | handler.setFormatter(SystemdFormatter()) 177 | 178 | logger = logger or logging.getLogger() 179 | logger.addHandler(handler) 180 | 181 | return systemd_ok 182 | -------------------------------------------------------------------------------- /tests/test_module.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import os 3 | from sys import version_info 4 | 5 | from systemdlogging.toolbox import init_systemd_logging 6 | 7 | LINE_NO = '19' if version_info >= (3, 8) else '25' 8 | 9 | 10 | def raiseme(): 11 | logger = logging.getLogger('mysystemdlogger') 12 | logger.setLevel(logging.DEBUG) 13 | 14 | try: 15 | raise ValueError('durutum %s yes') # this line will be in a traceback with % 16 | 17 | except ValueError: 18 | 19 | logger.exception('My message', extra={ 20 | 'message_id': True, 21 | 'context': { 22 | 'FIELD1': 'one', 23 | 'FIELD2': 'two', 24 | } 25 | }, stack_info=True) 26 | 27 | return True 28 | 29 | 30 | def test_basic(monkeypatch): 31 | 32 | os.environ['INVOCATION_ID'] = '' 33 | 34 | assert not init_systemd_logging(syslog_id='logtest') 35 | 36 | os.environ['INVOCATION_ID'] = 'dummy-invocation-id' 37 | 38 | assert init_systemd_logging(syslog_id='logtest') 39 | 40 | send_log = [] 41 | 42 | def send_mock(*args): 43 | 44 | args = list(args) 45 | assert isinstance(args[0], bytes) 46 | assert args.pop() is None 47 | 48 | entry = {} 49 | for arg in args: 50 | key, _, val = arg.decode().partition('=') 51 | entry[key] = val 52 | 53 | send_log.append(entry) 54 | 55 | monkeypatch.setattr('systemdlogging.toolbox.send', send_mock) 56 | 57 | assert raiseme() 58 | 59 | assert len(send_log) == 1 60 | entry = send_log[0] 61 | 62 | assert entry['MESSAGE'] == 'My message' 63 | assert entry['PRIORITY'] == '3' 64 | assert entry['CODE_FILE'].endswith('tests/test_module.py') 65 | assert entry['CODE_LINE'] == LINE_NO 66 | assert entry['CODE_FUNC'] == 'raiseme' 67 | assert entry['CODE_MODULE'] == 'test_module' 68 | assert entry['LOGGER'] == 'mysystemdlogger' 69 | assert entry['SYSLOG_IDENTIFIER'] == 'logtest' 70 | assert entry['THREAD_ID'] 71 | assert entry['THREAD_NAME'] 72 | assert entry['PROCESS_NAME'] 73 | assert 'ValueError: durutum %%s yes' in entry['TRACEBACK'] 74 | assert f', line {LINE_NO}, in raiseme' in entry['STACK'] 75 | assert entry['MESSAGE_ID'] 76 | assert entry['FIELD1'] == 'one' 77 | assert entry['FIELD2'] == 'two' 78 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | # See http://tox.readthedocs.org/en/latest/examples.html for samples. 2 | [tox] 3 | envlist = 4 | py{36,37,38,39,310} 5 | 6 | skip_missing_interpreters = True 7 | 8 | install_command = pip install {opts} {packages} 9 | 10 | [testenv] 11 | commands = 12 | python setup.py test 13 | 14 | deps = 15 | 16 | --------------------------------------------------------------------------------