Download the PHP package marck-devs/simple-logger without Composer
On this page you can find all versions of the php package marck-devs/simple-logger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download marck-devs/simple-logger
More information about marck-devs/simple-logger
Files in marck-devs/simple-logger
Informations about the package simple-logger
Simple logger for php
Install
Install via composer:
Usage
LogLevels
This class contains all levels available for the logger:
- DEBUG
- LOG
- INFO
- WARN
- ERROR
- CRITICAL
Also have static method to parse string and get the corresponding level:
With this method you can read the log level from .env or config file and load into logger.
SimpleLogger
This is the main class.
Method: public function __construct($name, $level, $date_format, $log_format)
- $name is the name of the logger, defaults is app
- $level is the min log level that will show, default is
LogLevels::WARN
- $date_format is an DateFormatter interface implementation, defaults is SimpleDateFormatter
- $log_format is an LogForammtter interface implementation, defaults is SimpleFormatter
public function log($msg, $data=[])
public function info($msg, $data=[])
public function debug($msg, $data=[])
public function warn($msg, $data=[])
public function error($msg, $data=[])
public function critical($msg, $data=[])
Write a line in the default output stream whit the passed message. This message can be formatted as python format function. In the message can put any key into braces and generate the array as dictionary whit the key and the value:
There are same reserved keywords:
- {file} - get the file path
- {date} - the time date
- {user} - the current user
- {class} - the class that call logger
- {function} - the function that call logger
- {line} - the line from has been called
- {lvl} - the log level
- {msg} - the log message
If want a personal format for logger, you can create a class that implements LogFormatter.
Save into log files
The SimpleLogger has two method to set the logs file and the errors file:
With this the default output stream will change to the files.
Make custom formatter
The log formatter set the way to print the log line into the main output stream. To create custom, you will need to implement the LogFormatter
interface.
When you have your custon formatter you can set to the logger via static method or constructor:
In both case the formatter is set globally.
Formating the date
The default format for date is the Spanish format: d-m-Y H:i:s
, but this can be
change to custom format creatin an implementation from DateFormatter
interface.
To set to the logger, like before, you can set in the constructor or with the static method:
Set Log level
To set the log level for the logger we have the static method set_log_level(int)
It need a LogLevel constant.
File paths
When visualize the file path whit the {file}
option, dont show full path, only the
file name and same parent dir. The number of parent can be change by setting in the
static method set_dir_level(int)
. If is set 2 levels, only will show 2 dir parents.