I have severals servers logging to the central syslog server, which collect the logs using syslog-ng.

I send logs with a tag using the logger command :

$ logger -n "Hello from $HOST at $(date)" -t MY_SPECIFIC_TAG -p local2.error

Using syslog-ng, the message is then written as following :

Mar 5 23:36:04 MY_SPECIFIC_TAG: Hello from vps1234567 at Tue Mar 5 23:36:04 CET 2019

My aim is to make all applications (java, apache, mysql,...) send their logs using a tag that identify the app generating the message, so I can write the logs on my syslog-server in a file matching the name of the app.

Is it possible to extract the string MY_SPECIFIC_TAG from the message, and use it as a variable in the path of file ?

This extraction needs to be perform in the syslog-ng configuration file, so I can write the destinations as following:

destination dst_custom{ 

Ok, I solved it :

The syslog-ng macro ${PROGRAM} extract this information.


BLUF: Yes, it is.
Q: If you know the tag already, why do you need to extract it?
Posit 1: I know MY_SPECIFIC_TAG and want to extract it from the messages file:
grep MY_SPECIFC_TAG /var/log/messages | sed -n "1s/^[A-Z][a-z][a-z] [0-9:.]* (.): ./\1/p"
That ought to get you MY_SPECIFIC_TAG (or at least pretty close). YMMV
Posit 2: I know MY_SPECIFIC_TAG but I do not know the path in front of it (i.e. /path/to/program/MY_SPECIFIC_TAG)
grep MY_SPECIFC_TAG /var/log/messages | sed -n "1s+^[A-Z][a-z][a-z] [0-9:.]* (/[a-zA-Z0-9/]/.): .*+\1+p"

Is that what you are looking for?
To set that as a variable, place the entire command inside $() and use that as the right side of the assignment.
my_path_var=$( {command from above} )

UPDATE: If you don't know what MY_SPECIFIC_TAG is, and you want to find them:
cat /var/log/messages | sed -n "s/^[A-Z][a-z][a-z] [0-9:.]* (.): ./\1/p" | sort -u
This will not print any lines without a tag.

Now, before you go all "UUOC!" on me, I've done it this way to show the comparison of this command to the old command. That way, the OP (and other future readers) can easily tailor these commands to their system.
Note: UUOC = Unnecessary Use Of Cat. The file name can be placed after the p" and the cat command removed. However, for demonstrative purposes, cat is being used here.

  • This extraction needs to be done inside the syslog-ng configuration file, hence I cannot use Grep. Well I think. I added details to the OP. – Yann-Reun Mar 6 at 7:44
  • I'll update my answer to remove the grep, pull out all the tags and remove all the duplicates. – Scottie H Mar 7 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.