未加星标

Adjusting or silencing a systemd service or app’s logging levels

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

Different services and apps have different ideas of what is worth logging, and you may not agree with what they consider worthy of logging. Some apps and services are especially chatty in logs and you may want to silence them completely. Here are three methods for either adjusting or silencing overly chatty apps and services.

The systemd journal only have limited storage filtering capabilities, and defaults to store everything in case it might be useful. However, sometimes you really just want to silence a desktop app or a service unit entirely or at least adjust their logging verbosity.

Adjust logging level of systemd service

You can set a minimum logging level based on the standard syslog levels of individual systemd service units.

This method works in systemd version 236 and later, but only works for programs that send logs directly to journald or syslog, or output to stdout in syslog format.

Create a service unit override by editing the systemd service you want to modify by running the following command:

<kbd>systemctl edit example.service</kbd>

This opens a text editor where you need to add a LogLevelMax value. Below is an example override that restricts logging to level 3 (errors) and higher.

[Service]
LogLevelMax=3

For reference, the standard log levels are emergency (0), alert (1), critical (2), error (3), warning (4), notice (5), info (6), and debug (6). Setting a lower number excludes the higher and less important log messages from your journal.

Install the service override and restart the service by executing the following commands: <kbd>systemctl daemon-reload
systemctl restart systemd-reoslved.service</kbd>

The service will no longer log messages with a logging level higher than the maximum level you configured.

Silencing a service’s standard output

By default, systemd will forward every standard output (stdout) and error (stderr) message to the journal. Some programs aren’t really designed for this behavior and can be too chatty, especially on standard output. You can override this behavior and discard their outputs instead.

Create a service unit override by editing the systemd service you want to modify by running the following command:

<kbd>systemctl edit example.service</kbd>

This opens a text editor where you can direct the output forwarding to null instead of the journal.

[Service]
StandardOutput=null
#StandardError=null Install the service override and restart the service by executing the following commands: <kbd>systemctl daemon-reload
systemctl restart systemd-reoslved.service</kbd>

The service will no longer forward standard output messages toi the journal.

Silencing a desktop app

Depending on your desktop environment, your programs’ standard output (stdout) and error (stderr) messages may be forwarded to the journal. You can override this behavior by overriding the application definition file (the .desktop file) for programs you want to silence.

Start by copying the existing desktop file to your user’s applications folder (you may need to create this directory). It’s important that this file has the same name as the system-wide installation to avoid ending up with duplicate application entries.

<kbd>cp /usr/share/applications/example.desktop ~/.local/share/applications/</kbd>

Then, edit your own user’s copy of the file ( ~/.local/share/applications/example.desktop ) and modify the Exec statement to silence standard output.

The first example shows how to modify the Exec statement to silence just the standard output, and the second example shows how to silence standard output and error. (Changes from the original Exec statement are highlighted in blue.) Depending on the program, it may be necessary to silence standard error messages though be careful so you don’t end up silencing actually important error messages entirely.

Exec=<mark>bash -c "</mark>/usr/bin/exampleapp <mark>>/dev/null"</mark>

#Exec=<mark>bash -c "</mark>/usr/bin/exampleapp <mark>>/dev/null 2>&1"</mark>

You can of course setup more specific filtering of messages here using grep and other utilities, but this is out of scope for this tutorial.

Install the modified application definition file and update the application definition database. You’ll only need to execute one of these commands, but which one varies by desktop environment. You can just run both without any problems. <kbd>desktop-file-install ~/.local/share/applications/example.desktop
update-desktop-database ~/.local/share/applications/</kbd> Lastly, you’ll need to restart the app for the changes to take effect.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

代码区博客精选文章
分页:12
转载请注明
本文标题:Adjusting or silencing a systemd service or app’s logging levels
本站链接:https://www.codesec.net/view/628488.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(116)