Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Imldeployment
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IML Open Source
Imldeployment
Commits
3c9e168a
Commit
3c9e168a
authored
10 months ago
by
Hahn Axel (hahn)
Browse files
Options
Downloads
Patches
Plain Diff
update vendor logger class
parent
5a6fddf6
No related branches found
No related tags found
1 merge request
!66
php8 only; added variable types; short array syntax; remove glyphicons
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
public_html/deployment/classes/logger.class.php
+137
-121
137 additions, 121 deletions
public_html/deployment/classes/logger.class.php
with
137 additions
and
121 deletions
public_html/deployment/classes/logger.class.php
+
137
−
121
View file @
3c9e168a
...
@@ -27,9 +27,11 @@
...
@@ -27,9 +27,11 @@
* 2022-10-16 mark longest action with an icon
* 2022-10-16 mark longest action with an icon
* 2022-12-15 make it compatible to PHP 8.2; add doc + comments
* 2022-12-15 make it compatible to PHP 8.2; add doc + comments
* 2023-05-15 fix _getBar() - division by zero
* 2023-05-15 fix _getBar() - division by zero
* 2024-07-12 php8 only: use variable types; update phpdocs
* ----------------------------------------------------------------------
* ----------------------------------------------------------------------
*/
*/
class
logger
{
class
logger
{
/**
/**
* @var {array} array of added messages
* @var {array} array of added messages
...
@@ -51,21 +53,22 @@ class logger {
...
@@ -51,21 +53,22 @@ class logger {
*/
*/
protected
$sCssPrefix
=
''
;
protected
$sCssPrefix
=
''
;
protected
$sSourceUrl
=
'https://github.com/axelhahn/ahlogger'
;
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// CONSTRUCTOR
// CONSTRUCTOR
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
/**
/**
*
c
onstuctor
*
C
onstuctor
* @param string $sInitMessage init message
* @param string $sInitMessage init message
* @return boolean
*/
*/
public
function
__construct
(
$sInitMessage
=
"Logger was initialized."
)
{
public
function
__construct
(
string
$sInitMessage
=
"Logger was initialized."
)
{
$this
->
_iMemStart
=
memory_get_usage
();
$this
->
_iMemStart
=
memory_get_usage
();
$this
->
enableDebug
(
true
);
$this
->
enableDebug
(
true
);
$this
->
add
(
$sInitMessage
);
$this
->
add
(
$sInitMessage
);
$this
->
sCssPrefix
=
'debug-'
.
md5
(
microtime
(
true
));
$this
->
sCssPrefix
=
'debug-'
.
md5
(
microtime
(
true
));
return
true
;
}
}
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
...
@@ -73,12 +76,13 @@ class logger {
...
@@ -73,12 +76,13 @@ class logger {
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
/**
/**
*
a
dd a logging message
*
A
dd a logging message
* @param
type
$sMessage
* @param
string
$sMessage
* @param
type
$sLevel
* @param
string
$sLevel
* @return boolean
* @return boolean
*/
*/
public
function
add
(
$sMessage
,
$sLevel
=
"info"
)
{
public
function
add
(
string
$sMessage
,
string
$sLevel
=
"info"
):
bool
{
if
(
!
$this
->
bShowDebug
)
{
if
(
!
$this
->
bShowDebug
)
{
return
false
;
return
false
;
}
}
...
@@ -93,20 +97,22 @@ class logger {
...
@@ -93,20 +97,22 @@ class logger {
}
}
/**
/**
*
e
nable / disable debugging
*
E
nable / disable debugging
* @param
type
$bEnable
* @param
bool
$bEnable
* @return
type
* @return
bool
*/
*/
public
function
enableDebug
(
$bEnable
=
true
){
public
function
enableDebug
(
bool
$bEnable
=
true
):
bool
{
return
$this
->
bShowDebug
=
!!
$bEnable
;
return
$this
->
bShowDebug
=
!!
$bEnable
;
}
}
/**
/**
*
e
nable client debugging by a given array of allowed ip addresses
*
E
nable client debugging by a given array of allowed ip addresses
* @param array $aIpArray list of ip addresses in a flat array
* @param array $aIpArray list of ip addresses in a flat array
* @return boolean
* @return boolean
*/
*/
public
function
enableDebugByIp
(
$aIpArray
){
public
function
enableDebugByIp
(
array
$aIpArray
):
bool
{
$this
->
enableDebug
(
false
);
$this
->
enableDebug
(
false
);
if
(
!
$_SERVER
||
!
is_array
(
$_SERVER
)
||
!
array_key_exists
(
"REMOTE_ADDR"
,
$_SERVER
))
{
if
(
!
$_SERVER
||
!
is_array
(
$_SERVER
)
||
!
array_key_exists
(
"REMOTE_ADDR"
,
$_SERVER
))
{
return
false
;
return
false
;
...
@@ -114,10 +120,11 @@ class logger {
...
@@ -114,10 +120,11 @@ class logger {
if
(
array_search
(
$_SERVER
[
'REMOTE_ADDR'
],
$aIpArray
)
!==
false
)
{
if
(
array_search
(
$_SERVER
[
'REMOTE_ADDR'
],
$aIpArray
)
!==
false
)
{
$this
->
enableDebug
(
true
);
$this
->
enableDebug
(
true
);
}
}
return
true
;
}
}
/**
/**
*
h
elper function: prepare array of added massages before output
*
H
elper function: prepare array of added massages before output
* - detect warnings and errors
* - detect warnings and errors
* - detect needed time for each action
* - detect needed time for each action
* - detect longest action
* - detect longest action
...
@@ -126,7 +133,8 @@ class logger {
...
@@ -126,7 +133,8 @@ class logger {
*
*
* @return array
* @return array
*/
*/
protected
function
_prepareRendering
(){
protected
function
_prepareRendering
():
array
{
$iMem
=
memory_get_usage
();
$iMem
=
memory_get_usage
();
$this
->
add
(
'<hr>'
);
$this
->
add
(
'<hr>'
);
$this
->
add
(
'Memory on start: '
.
number_format
(
$this
->
_iMemStart
,
0
,
'.'
,
','
)
.
" bytes"
);
$this
->
add
(
'Memory on start: '
.
number_format
(
$this
->
_iMemStart
,
0
,
'.'
,
','
)
.
" bytes"
);
...
@@ -202,12 +210,13 @@ class logger {
...
@@ -202,12 +210,13 @@ class logger {
}
}
/**
/**
*
g
et html code for a progressbar with divs
*
G
et html code for a progressbar with divs
* @param
{
int|float
}
$iVal value between 0..max value
* @param int|float $iVal value between 0..max value
* @param
{
int|float
}
$iMax max value
* @param int|float $iMax max value
* @return
{
string
}
* @return string
*/
*/
protected
function
_getBar
(
$iVal
,
$iMax
){
protected
function
_getBar
(
int
|
float
$iVal
,
int
|
float
$iMax
):
string
{
return
$iMax
>
0
return
$iMax
>
0
?
'<div class="bar"><div class="progress" style="width: '
.
(
$iVal
/
$iMax
*
100
)
.
'%;"> </div></div>'
?
'<div class="bar"><div class="progress" style="width: '
.
(
$iVal
/
$iMax
*
100
)
.
'%;"> </div></div>'
:
''
:
''
...
@@ -215,9 +224,11 @@ class logger {
...
@@ -215,9 +224,11 @@ class logger {
}
}
/**
/**
* render output of all logging messages
* Render output of all logging messages
* @return string
*/
*/
public
function
render
()
{
public
function
render
():
string
{
if
(
!
$this
->
bShowDebug
)
{
if
(
!
$this
->
bShowDebug
)
{
return
false
;
return
false
;
}
}
...
@@ -257,39 +268,40 @@ class logger {
...
@@ -257,39 +268,40 @@ class logger {
foreach
(
$aData
[
'entries'
]
as
$aLogentry
)
{
foreach
(
$aData
[
'entries'
]
as
$aLogentry
)
{
$sOut
.
=
'<tr class="'
.
$this
->
sCssPrefix
.
'-level-'
.
$aLogentry
[
"level"
]
.
''
.
(
$aLogentry
[
"trid"
]
==
$aData
[
"maxrowid"
]
?
' '
.
$this
->
sCssPrefix
.
'-maxrow'
:
''
)
.
'" '
$sOut
.
=
'<tr class="'
.
$this
->
sCssPrefix
.
'-level-'
.
$aLogentry
[
"level"
]
.
''
.
(
$aLogentry
[
"trid"
]
==
$aData
[
"maxrowid"
]
?
' '
.
$this
->
sCssPrefix
.
'-maxrow'
:
''
)
.
'" '
.
'id="'
.
$aLogentry
[
"trid"
]
.
'">'
.
.
'id="'
.
$aLogentry
[
"trid"
]
.
'">'
.
'<td>'
.
$aLogentry
[
"counter"
]
.
'</td>'
.
'<td
align="right"
>'
.
$aLogentry
[
"counter"
]
.
'</td>'
.
'<td>'
.
$aLogentry
[
"level"
]
.
'</td>'
.
'<td>'
.
$aLogentry
[
"level"
]
.
'</td>'
.
'<td>'
.
$aLogentry
[
"timer"
]
.
'</td>'
.
'<td
align="right"
>'
.
$aLogentry
[
"timer"
]
.
'</td>'
.
'<td>'
.
$this
->
_getBar
(
$aLogentry
[
"delta"
],
$aData
[
"maxtime"
]
*
1000
)
.
$aLogentry
[
"delta"
]
.
' ms'
.
(
$aLogentry
[
"delta"
]
==
$aData
[
'maxtime'
]
*
1000
?
'
⏱️'
:
''
)
.
'
</td>'
.
'<td
align="right"
>'
.
$this
->
_getBar
(
$aLogentry
[
"delta"
],
$aData
[
"maxtime"
]
*
1000
)
.
(
$aLogentry
[
"delta"
]
==
$aData
[
'maxtime'
]
*
1000
?
'⏱️
'
:
''
)
.
$aLogentry
[
"delta"
]
.
' ms
</td>'
.
'<td>'
.
$this
->
_getBar
(
$aLogentry
[
"memory"
],
$aData
[
"maxmem"
])
.
$aLogentry
[
"memory"
]
.
' MB'
.
'</td>'
.
'<td
align="right"
>'
.
$this
->
_getBar
(
$aLogentry
[
"memory"
],
$aData
[
"maxmem"
])
.
$aLogentry
[
"memory"
]
.
' MB'
.
'</td>'
.
'<td>'
.
$aLogentry
[
"message"
]
.
'</td>'
.
'<td>'
.
$aLogentry
[
"message"
]
.
'</td>'
.
'</tr>'
;
'</tr>'
;
}
}
if
(
$sOut
)
{
if
(
$sOut
)
{
$sOut
=
'
$sOut
=
'
<style>
<style>
.'
.
$this
->
sCssPrefix
.
'-info
{position: fixed; top: 6em; right: 1em; background: rgba(2
0
0,2
28
,255, 0.8); border:
1
px solid; z-index: 99999;}
.'
.
$this
->
sCssPrefix
.
'-info {position: fixed; top: 6em; right: 1em; background: rgba(2
3
0,2
40
,255, 0.8); border:
2
px solid
rgba(0,0,0,0.2); border-radius: 0.3em
; z-index: 99999;}
.'
.
$this
->
sCssPrefix
.
'-info .head {background: rgba(0,0,0,0.4); color: #fff;padding: 0em 0.5em 0.2em; }
.'
.
$this
->
sCssPrefix
.
'-info .
logger
head {background: rgba(0,0,0,0.4); color: #fff;padding: 0em 0.5em 0.2em;
border-radius: 0.3em 0.3em 0 0;
}
.'
.
$this
->
sCssPrefix
.
'-info .content {padding: 0.5em; }
.'
.
$this
->
sCssPrefix
.
'-info .
logger
content {padding: 0.5em; }
.'
.
$this
->
sCssPrefix
.
'-info .content .total {font-size: 160%; color: rgba(0,0,0,0.5); margin: 0.3em 0; display: inline-block;}
.'
.
$this
->
sCssPrefix
.
'-info .
logger
content .total {font-size: 160%; color: rgba(0,0,0,0.5); margin: 0.3em 0; display: inline-block;}
.'
.
$this
->
sCssPrefix
.
'-messages {margin: 5em 2em 2em;}
.'
.
$this
->
sCssPrefix
.
'-messages {margin: 5em 2em 2em;}
.'
.
$this
->
sCssPrefix
.
'-messages>h3 {font-size: 150%; margin: 0 0 0.5em 0;}
.'
.
$this
->
sCssPrefix
.
'-messages .bar {background: rgba(0,0,0,0.03); height: 1.4em; position: absolute; width: 6em; border-right: 1px solid rgba(0,0,0,0.2);}
.'
.
$this
->
sCssPrefix
.
'-messages .bar {background: rgba(0,0,0,0.03); height: 1.4em; position: absolute; width: 6em; border-right: 1px solid rgba(0,0,0,0.2);}
.'
.
$this
->
sCssPrefix
.
'-messages .progress {background: rgba(100,140,180,0.2); height: 1.4em; padding: 0;}
.'
.
$this
->
sCssPrefix
.
'-messages .progress {background: rgba(100,140,180,0.2); height: 1.4em; padding: 0;
float: left;
}
.'
.
$this
->
sCssPrefix
.
'-messages table{background: #fff; color: #222;table-layout:fixed; }
.'
.
$this
->
sCssPrefix
.
'-messages table{background: #fff; color: #222;table-layout:fixed;
border: 2px solid rgba(0,0,0,0.2); border-radius: 0.5em;
}
.'
.
$this
->
sCssPrefix
.
'-messages table th{background: none;}
.'
.
$this
->
sCssPrefix
.
'-messages table th{background: none;
color: #222; border-bottom: 2px solid rgba(0,0,0,0.4);
}
.'
.
$this
->
sCssPrefix
.
'-messages table th.barcol{min-width: 7em; position: relative;}
.'
.
$this
->
sCssPrefix
.
'-messages table th.barcol{min-width: 7em; position: relative;}
.'
.
$this
->
sCssPrefix
.
'-messages table td{padding: 3px; vertical-align: top;}
.'
.
$this
->
sCssPrefix
.
'-messages table td{padding: 3px; vertical-align: top;}
.'
.
$this
->
sCssPrefix
.
'-messages table th:hover{background:#aaa !important;}
.'
.
$this
->
sCssPrefix
.
'-messages table th:hover{background:#aaa !important;}
.'
.
$this
->
sCssPrefix
.
'-level-info{background: #
e0e8f8
; color:#124}
.'
.
$this
->
sCssPrefix
.
'-level-info{background: #
f0f4f4
; color:#124}
.'
.
$this
->
sCssPrefix
.
'-level-warning{background: #fcf8e3; color: #980;}
.'
.
$this
->
sCssPrefix
.
'-level-warning{background: #fcf8e3; color: #980;}
.'
.
$this
->
sCssPrefix
.
'-level-error{background: #fce0e0; color: #944;}
.'
.
$this
->
sCssPrefix
.
'-level-error{background: #fce0e0; color: #944;}
.'
.
$this
->
sCssPrefix
.
'-maxrow{color:#f33; font-weight: bold;}
.'
.
$this
->
sCssPrefix
.
'-maxrow{color:#f33; font-weight: bold;}
</style>
</style>
<div class="'
.
$this
->
sCssPrefix
.
' '
.
$this
->
sCssPrefix
.
'-info '
.
$this
->
sCssPrefix
.
'-level-'
.
$aData
[
'level'
]
.
'
">
<div class="'
.
$this
->
sCssPrefix
.
' '
.
$this
->
sCssPrefix
.
'-info '
.
$this
->
sCssPrefix
.
'-level-'
.
$aData
[
'level'
]
.
'" onclick="location.href=\'#'
.
$this
->
sCssPrefix
.
'-messages\';
">
<div class="head">ahLogger</div>
<div class="
logger
head">ahLogger</div>
<div class="content">
<div class="
logger
content">
<span class="total">⏱️ '
.
$aData
[
'totaltime'
]
.
' s</span><br>
<span class="total">⏱️ '
.
$aData
[
'totaltime'
]
.
' s</span><br>
🪲 <a href="#'
.
$this
->
sCssPrefix
.
'-messages">Debug infos</a> | 🔺 <a href="#">top</a><br>
🪲 <a href="#'
.
$this
->
sCssPrefix
.
'-messages">Debug infos</a> | 🔺 <a href="#">top</a><br>
<span>longest action: ⏱️ <a href="#'
.
$aData
[
'maxrowid'
]
.
'">'
.
(
$aData
[
'maxtime'
]
*
1000
)
.
' ms</a></span>
<span>longest action: ⏱️ <a href="#'
.
$aData
[
'maxrowid'
]
.
'">'
.
(
$aData
[
'maxtime'
]
*
1000
)
.
' ms</a></span>
...
@@ -299,7 +311,7 @@ class logger {
...
@@ -299,7 +311,7 @@ class logger {
</div>
</div>
<div id="'
.
$this
->
sCssPrefix
.
'-messages" class="'
.
$this
->
sCssPrefix
.
' '
.
$this
->
sCssPrefix
.
'-messages">
<div id="'
.
$this
->
sCssPrefix
.
'-messages" class="'
.
$this
->
sCssPrefix
.
' '
.
$this
->
sCssPrefix
.
'-messages">
DEBUG :: LOG MESSAGES<br
>'
<h3>ahLogger 🪳 Debug messages</h3
>'
.
(
$aData
[
'errors'
]
?
'<span>Errors: '
.
$aData
[
'errors'
]
.
'</span><br>'
:
''
)
.
(
$aData
[
'errors'
]
?
'<span>Errors: '
.
$aData
[
'errors'
]
.
'</span><br>'
:
''
)
.
(
$aData
[
'warnings'
]
?
'<span>Warnings: '
.
$aData
[
'warnings'
]
.
'</span><br>'
:
''
)
.
(
$aData
[
'warnings'
]
?
'<span>Warnings: '
.
$aData
[
'warnings'
]
.
'</span><br>'
:
''
)
.
'<br>
.
'<br>
...
@@ -313,16 +325,20 @@ class logger {
...
@@ -313,16 +325,20 @@ class logger {
<th class="barcol">memory</th>
<th class="barcol">memory</th>
<th>message</th>
<th>message</th>
</tr></thead><tbody>
</tr></thead><tbody>
'
.
$sOut
.
'</tbody></table>'
'
.
$sOut
.
'</tbody></table>'
.
'🌐 <a href="'
.
$this
->
sSourceUrl
.
'" target="_blank">'
.
$this
->
sSourceUrl
.
'</a>'
;
;
}
}
return
$sOut
;
return
$sOut
;
}
}
/**
/**
*
r
ender output of all logging messages for cli output
*
R
ender output of all logging messages for cli output
* @return string
* @return string
*/
*/
public
function
renderCli
(){
public
function
renderCli
():
string
{
if
(
!
$this
->
bShowDebug
)
{
if
(
!
$this
->
bShowDebug
)
{
return
false
;
return
false
;
}
}
...
...
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