How do I find my program name?

2024/4/15 1:44:13

Problem

I am unable to write to a different log than the default one using syslog. I am unsure if maybe my app name is wrong in my configuration. Do "program name" and "process name" not mean the same thing? If not, how can I find my program name in Python 3.6?

Attempted

I have written a small application in Python 3.6. I am already successfully writing to a common syslog file in CentOS 7 at /var/log/messages with it. If I open that file, I can see my entries.

If I run ps aux | grep myappname, the process name is listed as python myappname.py.

I have created a file at /etc/rsyslog.d/00-myconfig.conf which contents are:

if $programname contains 'myappname' then /home/user/test_log.log
& ~

I have restarted the process with sudo systemctl restart rsyslog.

I have run rsyslogd -N1 which gives me a deprecation warning about my use of ~ in my config, proving that the config is being recognized. I have tried removing that line as I am not sure what it does, but that does not help either.

I can confirm that the entries are still being written to /var/log/messages. The entries are not going up in smoke.

Answer

As syslog does not include the appname in a log naturally, you need to add it yourself when creating a log message. Here an example using the Linux logger command with --tag.

logger --tag="myappname" "Some message"

So before adjusting anything, check if yourrsyslog.conf includes your files from /etc/rsyslog.d/ (because they get checked even if you do not include them in your rsyslog.conf.

# Include all config files in /etc/rsyslog.d/
include(file="/etc/rsyslog.d/*.conf")

If the error persists, then your query has to be adjusted. The easiest way to do this, is using property-based filters.

:syslogtag, isequal, "myappname" /var/log/test_log.log&~*.* /var/log/messages

The config checks if myappname equals the hostname in the log. If so, it is written to test_log.log. The next line (&~) then discards all messages that have been written. Thus, no additional rules will be applied to these messages. As such, they will not be written to /var/log/messages.

Important Note: When using the compare operation isequal, the two values that are compared must be exactly equal (case-sensitive) to match. See the rsyslog documentation for more information on property-based filters.


P.S. Checkout this post for further explanation on what &~ means in rsyslog.

https://en.xdnf.cn/q/73305.html

Related Q&A

Is it possible to automate the execution of a Python script using Microsoft Flow?

I want to execute a snippet of python code based on some trigger using Microsoft-Flow. Is there a way to do this? Basically I am exploring on Powerapps and Microsoft-Flow. I have data in powerapp, I c…

Python: Selecting numbers with associated probabilities [duplicate]

This question already has answers here:Closed 13 years ago.Possible Duplicates:Random weighted choiceGenerate random numbers with a given (numerical) distribution I have a list of list which contains …

Weights and Biases: Login and network errors

I recently installed Weights and Biases (wandb) for recording the metrics of my machine learning projects. Everything worked fine when connected to wandb cloud instance or when I used a local docker im…

Python: Opening a file without creating a lock

Im trying to create a script in Python to back up some files. But, these files could be renamed or deleted at any time. I dont want my script to prevent that by locking the file; the file should be abl…

Tensorflow dataset questions about .shuffle, .batch and .repeat

I had a question about the use of batch, repeat and shuffle with tf.Dataset.It is not clear to me exactly how repeat and shuffle are used. I understand that .batch will dictate how many training exampl…

How to sort in python with multiple conditions?

I have a list with sublists as follows:result = [ [helo, 10], [bye, 50], [yeah, 5], [candy,30] ]I want to sort this with three conditions: first, by highrest integer in index 2 of sublist, then by leng…

Not able to convert Numpy array to OpenCV Mat in Cython when trying to write c++ wrapper function

I am trying to implement cv::cuda::warpPerspective in python2, there is a very sweet post about how to do that here: link. I followed the instruction as described in that post, however, I got Segmentat…

Installing python tables on mac with m1 chip

I am trying to use tables in python3 on a new mac mini with the M1 chip. I am getting multiple errors when running HDF5_DIR=/opt/homebrew/Cellar/hdf5/1.12.0_1 pip3 install tablesERROR: Command errored …

Write unbuffered on python 3

Im trying to create a file on python without buffer, so its written at the same time I use write(). But for some reason I got an error. This is the line Im using: my_file = open("test.txt", &…

which should I use (for python-based sites)? sass, compass, switchcss...alternatives? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…