Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
php-modules
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
library
php-modules
Commits
9c26585a
Commit
9c26585a
authored
6 years ago
by
Hoop, Bert Jan de
Browse files
Options
Downloads
Patches
Plain Diff
L05DOCBST-41 add LoggerTrait trait
this is a copy of the LoggerTrait trait from the wms-api
parent
e10ede99
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/library/LoggerTrait.php
+112
-0
112 additions, 0 deletions
src/library/LoggerTrait.php
with
112 additions
and
0 deletions
src/library/LoggerTrait.php
0 → 100644
+
112
−
0
View file @
9c26585a
<?php
namespace
Library
;
/**
* Logger trait to add logging functionality to a class.
* If no logger is set all logging methods, error(), info(), etc. do nothing.
* The context for a log message can be provided with the ndc_push() and ndc_pop methods.
* NDC stands for Nested Diagnostic Context
*
* To use this trait add
* use \Library\Logger;
* at the begin of your class declaration.
*/
trait
LoggerTrait
{
/**
* PSR-3 compatible logger instance, if not set no logging is done
* @var \Psr\Log\LoggerInterface
*/
protected
$logger
;
/**
* nested diagnostic context, used as context for log messages
* @var array
*/
protected
$ndc_context
=
[];
/**
* set logger to send log messages to
* @param \Psr\Log\LoggerInterface $logger
*/
public
function
set_logger
(
\Psr\Log\LoggerInterface
$logger
)
{
$this
->
logger
=
$logger
;
return
$this
;
}
/**
* debug message
* @param string $message
* @param array $context if not set the ndc is used
* @return type
*/
public
function
debug
(
string
$message
,
array
$context
=
null
)
{
if
(
!
isset
(
$this
->
logger
))
{
return
;
}
$this
->
logger
->
debug
(
$message
,
isset
(
$context
)
?
$context
:
$this
->
ndc_context
);
}
/**
* info message
* @param string $message
* @param array $context if not set the ndc is used
* @return type
*/
public
function
info
(
string
$message
,
array
$context
=
null
)
{
if
(
!
isset
(
$this
->
logger
))
{
return
;
}
$this
->
logger
->
info
(
$message
,
isset
(
$context
)
?
$context
:
$this
->
ndc_context
);
}
/**
* warning message
* @param string $message
* @param array $context if not set the ndc is used
* @return type
*/
public
function
warning
(
string
$message
,
array
$context
=
null
)
{
if
(
!
isset
(
$this
->
logger
))
{
return
;
}
$this
->
logger
->
warning
(
$message
,
isset
(
$context
)
?
$context
:
$this
->
ndc_context
);
}
/**
* error message
* @param string $message
* @param array $context if not set the ndc is used
* @return type
*/
public
function
error
(
string
$message
,
array
$context
=
null
)
{
if
(
!
isset
(
$this
->
logger
))
{
return
;
}
$this
->
logger
->
error
(
$message
,
isset
(
$context
)
?
$context
:
$this
->
ndc_context
);
}
/**
* add message to the ndc (nested diagnostic context)
* @param string $message
*/
public
function
ndc_push
(
string
$message
){
array_push
(
$this
->
ndc_context
,
$message
);
}
/**
* remove last message from the ndc (nested diagnostic context)
*/
public
function
ndc_pop
(){
array_pop
(
$this
->
ndc_context
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment