Download the PHP package devdot/monolog-parser without Composer
On this page you can find all versions of the php package devdot/monolog-parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download devdot/monolog-parser
More information about devdot/monolog-parser
Files in devdot/monolog-parser
Informations about the package monolog-parser
Monolog Parser
A library for parsing monolog logfiles.
This library is compatible with Monolog 2 and provides parse options for multiline logs and Laravel logfiles.
Installation
Install the library using composer:
Basic Usage
Example for parsing a logfile test.log
using the default options:
You can create a new Parser
object by using the static function new
as well:
Documentation
Parser instance
Create new Parser
instances:
For parameters, see Method Reference.
Most methods return the Parser
instance itself, allowing the calls to be chained in one line as seen in the examples below.
Every Parser
is meant to be linked to only one file, however that is not a hard restriction and depends on your usage.
Files and Ready State
The parser has to be provided a filename (or a string, see constructor or with setFile
. Check, whether a parser is ready with isReady
:
Parsing
Parsing happens either when parse
or get
are called. The Parser
object will store the results and return them from cache when get
is called unless parse
is called again directly or clear
is called.
Clear the cached records:
Parse a string instead of a file:
Log Records
The log records are returned as a Log
object by get
. The Log
is a readonly array containing the records of type LogRecord
. These objects are readonly too and can be accessed like this:
You may also access the LogRecord
properties like array keys: $record['datetime']
instead of $record->datetime
.
For reference, Monolog log records look like this:
Patterns
Besides the default pattern, there are other patterns provided as listed below. Patterns have to have named subpatterns to work correctly. You can set any pattern like this:
PATTERN_MONOLOG2
(default): will parse most configurations of Monolog'sLineFormatter
pattern.PATTERN_MONOLOG2_MULTILINE
: will parse most configurations of Monolog'sLineFormatter
with multiline support.PATTERN_LARAVEL
: will parse Laravel logfiles that were created with the Monolog configuration that is provided by Laravel.
Parsing Options
The parsing process can be modified with the following options:
Option | Description |
---|---|
OPTION_SORT_DATETIME |
Sort the records by their timestamps in descending order. |
OPTION_JSON_AS_TEXT |
Return the JSON of context and extra as string instead of decoded JSON. Use this option if your logfiles have context or extra fields that cannot be decoded with json_decode . Has priority over OPTION_JSON_FAIL_SOFT . |
OPTION_JSON_FAIL_SOFT |
Return the JSON of context and extra as string only if exceptions during JSON decode happen. Use this option if your logfiles may have context or extra fields that cannot be decoded with json_decode , or if you don't want to handle exceptions. Has priority over OPTION_SKIP_EXCEPTIONS . |
OPTION_SKIP_EXCEPTIONS |
Skip any exceptions that occur during parsing. For example, if the JSON of context cannot be decoded, it will return null and will not throw a LogParsingException . |
OPTION_NONE |
Reset to default options. |
The options may be set like this:
Exceptions
Monolog-Parser will throw exceptions as listed below.
FileNotFoundException
: Whenever a file is accessed that does not exist, this exception will be thrown. Note: this will happen, whensetFile
is called with a non-existing file.ParserNotReadyException
: Wheneverparse
orget
are called while the parser is not ready (isReady()
isfalse
), this exception will be thrown. This is not the case ifparse
is provided with a string or when a parsing option prevents exceptions.LogParsingException
: Whenever the parsing of a logfile or log record fails, this exception will be thrown. See Limits and failing logs
Limits and failing logs
If reading your logs fails, please make sure the logs are generated correctly and not listed on the list below. If files generated by the default LineFormatter
pattern of Monolog fail to be parsed, please let me know and submit an issue. If you are using non-default patterns, I would still be interested to provide parse patterns if they are commonly used.
Log files or records that can (currently) not be parsed by Monolog-Parser:
- Invalid JSON in the context/extra section of a log entry (will throw a
LogParsingException
). - Valid JSON with misleading closing brackets like all listed in this other testfile will not throw an error.
Method Reference
If not stated otherwise, methods return the object $this
.
Method | Parameters | Description |
---|---|---|
__construct |
string $filename : see setFile |
Create a new instance of Parser . |
::new |
string $filename : see setFile |
Create a new instance (equivalent to __construct). |
setFile |
string $filename : full path to a valid file |
Set the file at $filename . If the files does not exist, this will throw a FileNotFoundException . |
setPattern |
string $pattern = '' : regex pattern that will be used by parse . |
Set the pattern that will be used by parse when parsing the logfile. The regex needs to be valid PHP regex with named subpatterns (see Patterns). |
setOptions |
int $options : options for the parser configuration. |
Set parsing options with the provided option flags that are listed here. |
isReady |
Returns true when the parser is ready to parse, otherwise false . This state requires that an existing, readable file to be set. |
|
parse |
string $string: optional string to be parsed instead of a file. |
This will parse the given string or the set file (see setFile ) if $string is empty. |
get |
bool $returnFromCache = true : optional bool to decided whether cache from previous parse or get should be returned. |
Get the results of the last parse . If no results exist, parse will be called internally. Set $returnFromCache to false if you do not want the records to be loaded from cache but to re-parse the file. |
clear |
Clear the cache from previous parse and rewind the file if it was already read. |
About
Requirements
- Monolog-Parser works with PHP 8.2 or above.
- Monolog-Parser does not require any Monolog package.
License
Monolog-Parser is licensed under the MIT License. See the LICENSE file for details.
Acknowledgements
This library was inspired by ddtraceweb's monolog parser and haruncpi's laravel log reader.