Download the PHP package unifreak/qlog without Composer
On this page you can find all versions of the php package unifreak/qlog. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download unifreak/qlog
More information about unifreak/qlog
Files in unifreak/qlog
Package qlog
Short Description configurable logger to log into redis queue for persistence or into stashed for debugging
License MIT
Homepage https://www.github.com/UniFreak/QLog
Informations about the package qlog
中文用户请见 https://github.com/UniFreak/QLog/blob/master/README.cn.md
QLog
wraps around Monolog
package, and added some customized Handlers/Processors/Formatters, to provide these features:
- Log records into stash zone (for latter output and debugging) or into redis queue, according to configs
- Provide some pre-defined log channels and corresponding methods to log into specified channels
- All records have additional fields, provide info about time consuming and memory usage. See Some Conceptions
- Auto generate a session key for each record. See Some Conceptions
- Tag records with
_id
identifiers - Search stashed records
For now, two main use case of QLog
are:
- Print stashed records in api response, for realtime debugging
- Process redis queued records asynchronously, aggregate them into one place (like ES) for search or visualization
Installation
Simply run composer require unifreak/qlog
Usage
Some Concepts
QLog
logs records as an array with a pre-defined structure, like this:
message
: Log messagecontext
: Log context-
level
&level_name
Log level, supported levels are
DEBUG
,INFO
,NOTICE
,WARNING
,ERROR
,CRITICAL
,ALERT
,EMERGENCY
. You can specify level by calling corresponding log methods. See Log -
channel
Log channel. Each log is in a specified channel, to indicate what application event this log maybe related to.
QLog has several pre-defined channels:
You can log records into pre-defined channels or your own customized channels, by calling differenct
in
methods. See Specify Channels datetime
: Log date timetime
/time_total
/mem
: Time consumed since last log / time consumed since first log / memory consumed-
session
:Log record's session key. This session key can be used to chain together logs in different requests which may cross several apps. Say a api call to applicatoin A
A:api/a
:A:api/a --> B:api/b --> C:api/c
If all logs in A, B and C share the same session key, then we can search for the entire cross-app log chains.
QLogger's session key is a randomized string. QLogger will read from
QLOG_SESSION
cookie to init the session key, if there is noQLOG_SESSION
cookie, then it will generate a new one. After this, all log records will have the same session key.But NOTE THIS: you have to maitain the
QLOG_SESSION
cookie manually, to chain up records. -
_id
: idIf we only have session key, this will be a problem: since session key is randomized, we don't know it before we searching the records chain, so how can we know this session key?
Here comes
id
field.The default log records structure doesn't have
_id
fields, but you can specify multiple_id
fields by callingidBy()
methods. See Specify ID.id
's main purpose, is to be searched firstly to locate one interested record, then use this record's session key to search the whole log records chain.
Initialisation
QLog's constructor requires two parameter: a \Predis\Client
/\Reids
instance, and a config array. Like this:
Config options: $config
-
queue_name
:Required, specify which redis queue for QLog to log into
-
default_channel
:Specify default channel. Default to
app
size
: Maximum redis queue size. Default to 3000log_to
: Where the records are logged into. Support three values:0
: Only log into stash zone1
: The default value. Only log into redis queue2
: Log into both stash and redis queue
Log
You can log different level record by calling different log methods, passing in log message (required) and log context (optional):
Specify Channels
You can specify pre-defined channels by calling these methods:
Also, you can specify a customized channel by calling in()
method:
Specify ID
You can specify multiple id name/value pairs by calling idBy()
method. NOTE: id name must end with _id
.
Then the record will have additional two id fields:
NOTE:
- If you call
idBy()
multiple times with the same name, the latter value will override the former - Id is sticky, this means that all logs afterwards will auto hold the specified id name/value.
Filter Stashed Logs
You can filter stashed logs by calling these methods:
NOTE: if QLog
is configured not to log into stash zone (the log_to
config option), then stash zone will be empty, hence all above methods will return a empty array
Laravel & Lumen
QLog provides a facade class and service provider class for laravel
/lumen
:
- Facade:
Unifreak\QLog\QLogFacade
- Service provider:
Unifreak\QLog\QLogServiceProvider
After registering facade and service provider, you can use QLog
to access QLogger
, like:
The QLogServiceProvider
also enables auto logging sql queries and GuzzleHttp
requests. You can use the following query parameters to control QLog
's log bahaviors:
-
qlog_debug
:A none-zero value is equivalent to pass in
qlog_autolog=2
andqlog_log_to=0
, enable auto logging bothGuzzleHttp
request and sql queries, and only log into stash zone. see below -
qlog_autolog
: control auto log behaviors1
: Default. Auto logGuzzleHttp
requests2
: Auto log bothGuzzleHttp
requests and sql queries
-
qlog_log_to
: control log zone0
: Only log into stash zone1
: Only log into redis queue2
: Log both into stash zone and redis queue
Register Facade and Service Provider
-
Add a new config file
config/qlog.php
: - Add the following codes into
bootstrap/app.php
:
NOTE: Make sure that service provider is registered after $app->withEloquent()
, otherwise the sql queries auto logging feature will not function properly
See laravel
/lumen
official documentation for more infomation
TODO
- Auto log exception in
QLogServiceProvider
- Auto send mails when record meet configured level