Parse::Syslog(3pm)

πŸ’– NAME πŸ’–

okay bestie, meet Parse::Syslog β€” it parses Unix syslog files. that's it, that's the whole job, and it does it beautifully. this is the 3pm page from the User Contributed Perl Documentation, so you know it's a Perl module situation. 🫢


🌸 SYNOPSIS 🌸

my $parser = Parse::Syslog->new( '/var/log/syslog', year => 2001);
while(my $sl = $parser->next) {
    ... access $sl->{timestamp|host|program|pid|text} ...
}

πŸ¦‹ DESCRIPTION πŸ¦‹

so like, Unix syslogs are genuinely lovely to read with your human eyeballs πŸ‘€ but because of small differences between operating systems and things like 'last message repeated xx times', they are not very easy to parse by a script. it's giving chaos, honestly.

Parse::Syslog presents a simple interface to parse syslog files: you create a parser on a file (with new) and call next to get one line at a time, with Unix-timestamp, host, program, pid and text returned in a hash-reference. one line at a time, no cap, we love a clean iterator. ✨

✨ Constructing a Parser ✨

new requires as first argument a source from where to get the syslog lines. it can be:

  • a file-name for the syslog-file to be parsed.
  • an IO::Handle object.
  • a File::Tail object as first argument, in which case the read method will be called to get lines to process.

after the file-name (or File::Tail object), you can specify options as a hash. the following options are defined:

  • type β€” Format of the "syslog" file. can be one of:
  • year β€” Syslog files usually do store the time of the event without year. with this option you can specify the start-year of this log. if not specified, it will be set to the current year. πŸ—“οΈ
  • GMT β€” If this option is set, the time in the syslog will be converted assuming it is GMT time instead of local time. (timezone bestie said no to your local clock ⏰)
  • repeat β€” Parse::Syslog will by default repeat xx times events that are followed by messages like 'last message repeated xx times'. if you set this option to false, it won't do that. so if you're seeing the same thing over and over, that's a feature, and you can turn it off.
  • arrayref β€” If this option is true, next will return an array-ref instead of a hash-ref (and is thus a bit faster πŸ”₯), with the following contents:
    • 0: timestamp
    • 1: host
    • 2: program
    • 3: pid
    • 4: text
  • locale β€” Optional. Specifies an additional locale name or the array of locale names for the parsing of log files with national characters. 🌍
  • allow_future β€” If true will allow for timestamps in the future. otherwise timestamps of one day in the future and more will not be returned (as a safety measure against wrong configurations, bogus --year arguments, etc.)
ngl the allow_future one is lowkey important β€” it's a guardrail against a wrong config or a messy --year, so don't be surprised when your future-dated lines just... don't come back. πŸ™ˆ

πŸŽ€ Parsing the file πŸŽ€

the file is parse one line at a time by calling the next method, which returns a hash-reference containing the following keys:

  • timestamp β€” Unix timestamp for the event.
  • host β€” Host-name where the event did happen.
  • program β€” Program-name of the program that generated the event.
  • pid β€” PID of the Program that generated the event. this information is not always available for every operating system. 😭
  • text β€” Text description of the event.
  • msgid β€” Message numeric identifier, available only on Solaris >= 8 with "message ID generation" enabled".
  • facility β€” Log facility name, available only on Solaris >= 8 with "message ID generation" enabled".
  • level β€” Log level, available only on Solaris >= 8 with "message ID generation" enabled".

πŸ’€ BUGS πŸ’€

there are many small differences in the syslog syntax between operating systems. this module has been tested for syslog files produced by the following operating systems:

Debian GNU/Linux 2.4 (sid)
Solaris 2.6
Solaris 8

report problems for these and other operating systems to the author. like, if your OS is doing something unhinged with its log format, pls tell him. πŸ“¬


Copyright (c) 2001, Swiss Federal Institute of Technology, Zurich. All Rights Reserved.

πŸ’… LICENSE πŸ’…

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

🌈 AUTHOR 🌈

David Schweikert <dws@ee.ethz.ch>

⭐ HISTORY ⭐

the glow-up timeline, fr fr:

 2001-08-12 ds 0.01 first version
 2001-08-19 ds 0.02 fix 'last message repeated xx times', Solaris 8 problems
 2001-08-20 ds 0.03 implemented GMT option, year specification, File::Tail
 2001-10-31 ds 0.04 faster time parsing, implemented 'arrayref' option, better time-increment algorithm
 2002-01-29 ds 0.05 ignore -- MARK -- lines, low-case months, space in program names
 2002-05-02 ds 1.00 HP-UX fixes, parse 'above message repeats xx times'
 2002-05-25 ds 1.01 added support for localized month names (uchum@mail.ru)
 2002-10-28 ds 1.02 fix off-by-one-hour error when running during daylight saving time switch
 2004-01-19 ds 1.03 do not allow future dates (if allow_future is not true)
 2004-07-11 ds 1.04 added support for type 'metalog'
 2005-12-24 ds 1.05 allow passing of a IO::Handle object to new

and that's the whole page, bestie!! πŸŽ€ this one's from perl v5.40.0, dated 2024-08-19, and honestly it ate. go parse some logs, you got this. mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from