Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webbasics
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
webbasics
Commits
121c77ff
Commit
121c77ff
authored
Jul 15, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added more dump options to Logger.
parent
ae736bed
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
5 deletions
+34
-5
logger.php
logger.php
+34
-5
No files found.
logger.php
View file @
121c77ff
...
@@ -9,6 +9,8 @@
...
@@ -9,6 +9,8 @@
namespace
WebBasics
;
namespace
WebBasics
;
require_once
'base.php'
;
/**
/**
* Logger class.
* Logger class.
*
*
...
@@ -16,13 +18,14 @@ namespace WebBasics;
...
@@ -16,13 +18,14 @@ namespace WebBasics;
*
*
* @package WebBasics
* @package WebBasics
*/
*/
class
Logger
{
class
Logger
extends
Base
{
const
CRITICAL
=
0
;
const
CRITICAL
=
0
;
const
ERROR
=
1
;
const
ERROR
=
1
;
const
WARNING
=
2
;
const
WARNING
=
2
;
const
INFO
=
3
;
const
INFO
=
3
;
const
DEBUG
=
4
;
const
DEBUG
=
4
;
static
$level_names
=
array
(
'CRITICAL'
,
'ERROR'
,
'WARNING'
,
'INFO'
,
'DEBUG'
);
static
$level_names
=
array
(
'CRITICAL'
,
'ERROR'
,
'WARNING'
,
'INFO'
,
'DEBUG'
);
private
static
$allowed_dump_formats
=
array
(
'plain'
,
'html'
,
'file'
);
const
DEFAULT_FORMAT
=
'%(datetime): %(level): %(message)'
;
const
DEFAULT_FORMAT
=
'%(datetime): %(level): %(message)'
;
...
@@ -30,6 +33,19 @@ class Logger {
...
@@ -30,6 +33,19 @@ class Logger {
private
$output
=
array
();
private
$output
=
array
();
private
$format
=
self
::
DEFAULT_FORMAT
;
private
$format
=
self
::
DEFAULT_FORMAT
;
private
$level
=
self
::
WARNING
;
private
$level
=
self
::
WARNING
;
private
$dump_format
=
'plain'
;
private
$log_directory
=
''
;
function
set_directory
(
$directory
)
{
$this
->
log_directory
=
self
::
path_with_slash
(
$directory
);
}
function
set_dump_format
(
$format
)
{
if
(
!
in_array
(
$format
,
self
::
$allowed_dump_formats
)
)
throw
new
InvalidArgumentException
(
sprintf
(
''
,
$format
));
$this
->
dump_format
=
$format
;
}
function
set_format
(
$format
)
{
function
set_format
(
$format
)
{
$this
->
format
=
(
string
)
$format
;
$this
->
format
=
(
string
)
$format
;
...
@@ -119,8 +135,18 @@ class Logger {
...
@@ -119,8 +135,18 @@ class Logger {
return
$output
;
return
$output
;
}
}
function
dump
()
{
function
dump
(
$file_prefix
=
'log'
)
{
echo
$this
->
dumps
();
switch
(
$this
->
dump_format
)
{
case
'plain'
:
echo
$this
->
dumps
();
break
;
case
'html'
:
echo
'<strong>Log:</strong><br />'
;
echo
'<pre>'
.
$this
->
dumps
()
.
'</pre>'
;
break
;
case
'file'
:
$this
->
save
(
sprintf
(
'%s_%s.log'
,
$file_prefix
,
strftime
(
'%d-%m-%Y_%H-%M-%S'
)));
}
}
}
function
clear
()
{
function
clear
()
{
...
@@ -128,7 +154,10 @@ class Logger {
...
@@ -128,7 +154,10 @@ class Logger {
}
}
function
save
(
$path
)
{
function
save
(
$path
)
{
file_put_contents
(
$path
,
$this
->
dumps
());
if
(
$this
->
log_directory
&&
!
is_dir
(
$this
->
log_directory
)
)
mkdir
(
$this
->
log_directory
,
0644
,
true
);
file_put_contents
(
$this
->
log_directory
.
$path
,
$this
->
dumps
());
}
}
function
handle_exception
(
\Exception
$e
)
{
function
handle_exception
(
\Exception
$e
)
{
...
@@ -138,7 +167,7 @@ class Logger {
...
@@ -138,7 +167,7 @@ class Logger {
$message
=
sprintf
(
"Uncaught %s in file %s, line %d: %s
\n\n
%s"
,
get_class
(
$e
),
$message
=
sprintf
(
"Uncaught %s in file %s, line %d: %s
\n\n
%s"
,
get_class
(
$e
),
$e
->
getFile
(),
$e
->
getLine
(),
$e
->
getMessage
(),
$e
->
getTraceAsString
());
$e
->
getFile
(),
$e
->
getLine
(),
$e
->
getMessage
(),
$e
->
getTraceAsString
());
$this
->
critical
(
$message
);
$this
->
critical
(
$message
);
$this
->
dump
();
$this
->
dump
(
'error'
);
}
}
function
set_as_exception_handler
()
{
function
set_as_exception_handler
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment