How to remove/modify syslogd message's header? - linux

I'm currently use the syslogd of busybox for logging some information. However, I'm unable to modify the message's header.
I log the message like this:
syslog(LOG_INFO,"My message\n");
And I got this output:
Jul 4 15:00:11 halo user.info syslog: My message
I want to replace message's header with epoch time format like this:
1529293692,My message
Or is there any way to completely remove the message's header so I could manually add the epoch time in code?

I have done the research around and found that it is impossible to modify the output message log of syslogd with supported configuration. So I dig into the busybox's source code and modify it. If you face the same issue, you could find it at function:
static void timestamp_and_log(int pri, char *msg, int len)
I check the pri variable to see which level log is it and change the actual outcome message, which is msg.

Related

NLog Layout with ${onexception} not displaying literal text

I have a Mail target to send an email for certain log entries. I would like the subject line to be prepended with "EXCEPTION THROWN:" (including the colon) if the log event includes an exception.
My subject line layout is:
${onexception:inner=EXCEPTION THROWN:}AppName Log Event
But the email sent has a subject line of:
AppName Log Event
None of the literal text within the ${onexception} layout renderer is included. And yes, I am sure an exception was passed. See below.
I thought maybe wrapping it in a literal renderer would help:
${onexception:inner=${literal:text=EXCEPTION THROWN:}}AppName Log Event
This gives me:
EXCEPTION THROWNAppName Log Event
That's better, but still missing the colon. It demonstrates that the issue isn't with a missing exception - the condition is being met because it's including at least some of the string. It's still stripping the colon, though.
Any ideas?
Ugh, nevermind. Posting here in case anyone else does what I did.
I needed to escape the ":". Once that was done, everything started showing up, even without using the ${literal} renderer:
${onexception:inner=EXCEPTION THROWN\:}AppName Log Event

Write to rsyslog with custom app name

What I have
I have written a Python class that writes messages to a custom log via rsyslog. This is scaling off of my previous question, which I have temporarily circumvented by prepending my app's name to each message and have rsyslog configured to put all logs containing my app's name to my custom log. However, I am concerned that something else on the system writes to rsyslog and that message just so happens to contain my app's name, rsyslog will detect this send that log entry to my log. I would also prefer the look of my app's name instead of journal appearing throughout my log.
Attached is my code:
import logging
from logging import handlers
lumberjack = logging.getLogger("MyAppName")
lumberjack.setLevel(logging.INFO)
handler = handlers.SysLogHandler(address='/dev/log')
handler.setFormatter(logging.Formatter('%(name)s %(levelname)s: %(message)s'))
handler.ident = "MyAppName"
lumberjack.addHandler(handler)
lumberjack.critical("I'm okay")
Goal
The following two messages are examples. The first was written by my Python class. The second was written by me running logger -t MyAppName -s "Hey buddy, I think you’ve got the wrong app name"
Aug 22 15:49:53 melchior journal: MyAppNameMyAppName CRITICAL: I'm okay.
Aug 22 15:57:06 melchior MyAppName: Hey buddy, I think you’ve got the wrong app name
Question
What do I have to change in my Python code to get these lines to look the same, but with the levelname included as I have already done so?
Change the following line
handler.setFormatter(logging.Formatter('%(name)s %(levelname)s: %(message)s'))
to look like this (basically just add a colon after %(name)s:
handler.setFormatter(logging.Formatter('%(name)s: %(levelname)s: %(message)s'))
then remove the following line to avoid app name duplication:
handler.ident = "MyAppName"
and now it does the trick:
Sep 10 06:52:33 hostname MyAppName: CRITICAL: I'm okay

logstash custom log that has xml tags inside

I have a custom log file that has plain text as well as xml tags. How do i capture these in separate fields. Here is how it looks like:
1/10/2017 4:16:35 AM :
Error thrown is:
No Error
Request sent is:
SCEO415154712
Response received is:
SCEO4151547trueTBAfalse7169-1TBAfalse2389-1
1/10/2017 4:16:35 AM :
Error thrown is:
No Error
*************************************************************************
Request sent is:
<InventoryMgmtRequest xmlns="http://www.af.com/Ecommerce/Worldwide/AvailabilityService/Schemas/InventoryMgmtRequest"><ns0:MsgHeader MessageType="FIXORD" MsgDate="10.01.2017 04:16:32" SystemOfOrigin="ISCS_DE" CommunityID="SG888" xmlns:ns0="http://www.av.com/Ecommerce/Worldwide/AvailabilityService/Schemas/InventoryMgmtRequest"><ns0:OrderID>SCEO4151547</ns0:OrderID><ns0:ReservationID></ns0:ReservationID><ns0:CRD></ns0:CRD></ns0:MsgHeader><ns0:MsgBody xmlns:ns0="http://www.ab.com/Ecommerce/Worldwide/AvailabilityService/Schemas/InventoryMgmtRequest"><ns0:Product Sku="CH562EE" Qty="1" IsExpress="false" IsTangible="true" Region="EMEA" Country="DE"><ns0:ProdType></ns0:ProdType><ns0:LineItemNum>1</ns0:LineItemNum><ns0:JCID></ns0:JCID></ns0:Product><ns0:Product Sku="CH563EE" Qty="1" IsExpress="false" IsTangible="true" Region="EMEA" Country="DE"><ns0:ProdType></ns0:ProdType><ns0:LineItemNum>2</ns0:LineItemNum><ns0:JCID></ns0:JCID></ns0:Product></ns0:MsgBody></InventoryMgmtRequest>
*************************************************************************
Response received is:
<ns0:InventoryMgmtResponse xmlns:ns0="http://www.ad.com/Ecommerce/Worldwide/AvailabilityService/Schemas/InventoryMgmtResponse"><ns0:MsgHeader MsgDate="10.01.2017 04:16:32" MessageType="FIXORD"><ns0:OrderID>SCEO4151547</ns0:OrderID><ns0:ReservationID /><ns0:ReadyToRelease>true</ns0:ReadyToRelease></ns0:MsgHeader><ns0:MsgBody><ns0:Product SKU="CH562EE" LSPSKU="9432GFT" OutOfStock="false" FulfillmentSite="00ZF" SKUExist="true" Region="EMEA" Country="DE" IsTangible="true"><ns0:EDD>TBA</ns0:EDD><ns0:FutureUsed>false</ns0:FutureUsed><ns0:CurrentQty>7169</ns0:CurrentQty><ns0:FutureQty>-1</ns0:FutureQty></ns0:Product><ns0:Product SKU="CH563EE" LSPSKU="9432GFU" OutOfStock="false" FulfillmentSite="00ZF" SKUExist="true" Region="EMEA" Country="DE" IsTangible="true"><ns0:EDD>TBA</ns0:EDD><ns0:FutureUsed>false</ns0:FutureUsed><ns0:CurrentQty>2389</ns0:CurrentQty><ns0:FutureQty>-1</ns0:FutureQty></ns0:Product></ns0:MsgBody></ns0:InventoryMgmtResponse>
*************************************************************************
Also I don't want to capture the line separators (line full of **** at the end) in my grok fields.
There is no simple answer here I'm afraid. Logstash and other log processing tools works line by line, each line is an event. If your events span more than one line you can use the multiline codec, which is pretty powerful, but in my experience you are better off trying to get the logs on to single lines at source, this makes it so much easier to write a pattern and get the process working reliably.
The issues you have here are many, but if, for example, one of your messages (sent via TCP) is retransmitted for some reason or simply (sent via UDP) lost, your pattern will break as part of the message that logstash is expecting is not there.
The best thing you can do in my opinion is to try and change the logging process to save to a file as a single line per event. Most logging tools should allow this with the right config options. Ideally, get your application to log in json format, (assuming you're processing logs to save them in elasticsearch) this would involve the lowest overhead on the logstash server to process these logs (as elasticsearch saves them in json format). All you would then need to do is pass each event/log line to the json filter and the fields are generated by the names given to it by your application.

Is there a printk-style log parser?

The journald of systemd supports kernel-style logging. So, the service can write on stderr the messages starting with "<6>", and they'll be parsed like info, "<4>" - warning.
But while developing the service it's launched outside of systemd. Is there any ready-to-use utilities to convert these numbers into readable colored strings? (it would be nice if that doesn't complicate the gdb workflow)
Don't want to roll my own.
There is no tool to convert the output but a simple sed run would do the magic.
As you said journal would strip off <x> token from the beginning of your log message and convert this to log level. What I would do is check for some env. variable in the code. For ex:
if (COLOR_OUTPUT_SET)
printf ("[ WARNING ] - Oh, snap\n");
else
printf ("<4> Oh, snap\n");

Unexpected ellipsis in syslog or splunk message

I am logging data to Splunk via log4j and a SyslogAppender. Sometimes the information shows up in Splunk with ellipsis (...) instead of the actual text with some odd spacing. When I log the same event to a RollingFileAppender, it logs normally. Would anyone know why this is happening and how to resolve it?
Thank you!
Example:
InboundTxnDate: 20130926 16:53:14:475
Out...
...boundTxnTypeCode: UNK
This is how syslog appenders work, they split log message, if it is bigger than 1019 bytes. When log message is split, this message will end with ellipsis and next message starts with ellipsis. Limit of message (1019 bytes) is hardcoded and cannot be changed by no configuration in log4j. Look at RFC 3164 and see source code of SyslogAppender
Would anyone know why this is happening
Ondřej Benkovský answered that very well and I have nothing to add.
how to resolve it?
If the split lines are part of the same event in Splunk, you can join them by editing $SPLUNK_HOME/etc/system/local/props.conf and adding a stanza like this:
[<spec>]
SEDCMD-join_log4j_syslog_lines=s/\.\.\.[\r\n]+\.\.\.//g
That will join the lines at index time and remove the ellipses.
Note that you'll need to change <spec> as detailed in the document for props.conf.
You can use the regex search command to make sure it's working:
| regex "\.\.\.[\r\n]+\.\.\."

Resources