0

I'm receiving my log without any parse:

source s_network {
     network( transport(tcp) port(601) flags(no-parse));
};

My saved log have two part:

  1. reciver information
  2. json part of log ( like below ):

Feb 18 00:01:58 82.172.112.1 <40> 2020-02-17T20:29:49Z logserver-2 : {"method":"GET","scheme":"https","domain":"test.com","uri":"/App_Themes/font.woff","referer":"https://test.com/","ip":"31.135.115.92","ua":"Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1","content_type":"text/html","status":404,"server_port":443,"bytes_sent":1222,"bytes_received":1245,"upstream_time":0.016}

My questions are:

How can I separate json part(remove first part of text)?

How can I generate file name base on json data? For example I want to save get method in the separate file.

4
  • Does every log entry starts with : "Feb 18 00:01:58 82.172.112.1 <40> 2020-02-17T20:29:49Z logserver-2 : " (or similiar) ? Also, what I see the second part is not valid JSON as well... look here: AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1""content_type" ... between Safari/604.1""content_type" there should be a "," which is not. Can you post some content with contiues logging on stackoverflow please ?
    – SmileMZ
    Feb 24, 2020 at 6:27
  • @SmileMZ you text of first question is yes here is some fisrt part : "Feb 23 12:21:00 93.115.150.119 <40> 2020-02-23T08:48:53Z logserver-3"
    – MHF
    Feb 24, 2020 at 11:09
  • @SmileMZ your second part is always a valid json . the problem in the qustion is my copy and paste fault
    – MHF
    Feb 24, 2020 at 11:11
  • @SmileMZ i think i must write a custom log pattern
    – MHF
    Feb 24, 2020 at 11:15

1 Answer 1

0

To separate the JSON from the other information it could be as easy as:

https://www.syslog-ng.com/technical-documents/doc/syslog-ng-open-source-edition/3.18/administration-guide/33

destination d_file {
    file("/var/log/${YEAR}.${MONTH}.${DAY}/json"
         template("${MESSAGE}\n")
         template-escape(no));
};

To access values in the JSON use the according parser:

https://www.syslog-ng.com/technical-documents/doc/syslog-ng-open-source-edition/3.16/release-notes/the-json-parser-the-json-parser

parser parser_name {
    json-parser(
        marker()
        prefix()
    );
};

And combine the parts to get your desired result:

source s_network {
     network( transport(tcp) port(601) );
};

destination d_date_method {
    file("/var/log/${YEAR}.${MONTH}.${DAY}/${method}.json"
         template("${MESSAGE}\n")
         template-escape(no));
};

parser p_json {
    json-parser();
};

log {
    source(s_network);
    parser(p_json);
    destination(d_date_method);
};

Make sure to disable the flag no-parse in the source:

https://www.syslog-ng.com/technical-documents/doc/syslog-ng-open-source-edition/3.16/administration-guide/para-flags-no-parse

1
  • "Make sure to disable the flag no-parse in the source:" this is the key . thanks
    – MHF
    Mar 17, 2020 at 10:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .