Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
logger-36
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
Package Registry
Model registry
Operate
Environments
Terraform modules
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
DEBREUVE Eric
logger-36
Commits
3559653c
Commit
3559653c
authored
2 years ago
by
DEBREUVE Eric
Browse files
Options
Downloads
Patches
Plain Diff
code factorization/simplification + fixed mem usage units + max mem usage
parent
d1717408
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
logger_36/__init__.py
+1
-0
1 addition, 0 deletions
logger_36/__init__.py
logger_36/main.py
+44
-31
44 additions, 31 deletions
logger_36/main.py
logger_36/test.py
+4
-1
4 additions, 1 deletion
logger_36/test.py
logger_36/version.py
+1
-1
1 addition, 1 deletion
logger_36/version.py
with
50 additions
and
33 deletions
logger_36/__init__.py
+
1
−
0
View file @
3559653c
...
@@ -34,6 +34,7 @@ from logger_36.main import (
...
@@ -34,6 +34,7 @@ from logger_36.main import (
AddFileHandler
,
AddFileHandler
,
ElapsedTime
,
ElapsedTime
,
LogSystemDetails
,
LogSystemDetails
,
MaximumMemoryUsage
,
SaveLOGasHTML
,
SaveLOGasHTML
,
SetShowMemoryUsage
,
SetShowMemoryUsage
,
)
)
...
...
This diff is collapsed.
Click to expand it.
logger_36/main.py
+
44
−
31
View file @
3559653c
...
@@ -46,11 +46,11 @@ try:
...
@@ -46,11 +46,11 @@ try:
from
psutil
import
Process
as
process_t
from
psutil
import
Process
as
process_t
_PROCESS
=
process_t
()
_PROCESS
=
process_t
()
_MEGA_
SCALING
=
1.0
/
(
1024.0
**
2
)
_MEGA_
UNIT
=
1024.0
**
2
_GIGA_
SCALING
=
_MEGA_
SCALING
/
1024.0
_GIGA_
UNIT
=
_MEGA_
UNIT
*
1024.0
except
ModuleNotFoundError
:
except
ModuleNotFoundError
:
_PROCESS
=
None
_PROCESS
=
None
_MEGA_
SCALING
=
_GIGA_
SCALING
=
0.0
_MEGA_
UNIT
=
_GIGA_
UNIT
=
0.0
# This module is certainly imported early. Therefore, the current time should be close enough to the real start time.
# This module is certainly imported early. Therefore, the current time should be close enough to the real start time.
...
@@ -64,7 +64,8 @@ _WHERE_SEPARATOR = "@"
...
@@ -64,7 +64,8 @@ _WHERE_SEPARATOR = "@"
_MESSAGE_FORMAT
=
(
_MESSAGE_FORMAT
=
(
f
"
%(asctime)s[%(levelname)s]
\t
-
"
f
"
%(asctime)s[%(levelname)s]
\t
-
"
f
"
%(message)s
{
_WHERE_SEPARATOR
}
"
f
"
%(message)s
{
_WHERE_SEPARATOR
}
"
f
"
%(module)s:%(funcName)s:%(lineno)d
"
f
"
%(module)s:%(funcName)s:%(lineno)d
"
f
"
+%(elapsed_time)s%(memory_usage)s
"
)
)
_NEXT_LINE_PROLOGUE
=
"
\n
"
+
(
27
+
_TAB_SIZE
)
*
"
"
_NEXT_LINE_PROLOGUE
=
"
\n
"
+
(
27
+
_TAB_SIZE
)
*
"
"
_DATE_FORMAT
=
"
%Y-%m-%d@%H:%M:%S
"
_DATE_FORMAT
=
"
%Y-%m-%d@%H:%M:%S
"
...
@@ -95,18 +96,18 @@ _MAX_DETAIL_NAME_LENGTH = max(map(len, _SYSTEM_DETAILS.keys()))
...
@@ -95,18 +96,18 @@ _MAX_DETAIL_NAME_LENGTH = max(map(len, _SYSTEM_DETAILS.keys()))
class
_handler_extension
:
class
_handler_extension
:
__slots__
=
(
"
formatter
"
,
"
show_memory_usage
"
)
__slots__
=
(
"
formatter
"
,
"
show_memory_usage
"
,
"
max_memory_usage
"
)
formatter
:
lggg
.
Formatter
formatter
:
lggg
.
Formatter
show_memory_usage
:
bool
show_memory_usage
:
bool
max_memory_usage
:
int
def
__init__
(
self
)
->
None
:
def
__init__
(
self
)
->
None
:
""""""
""""""
self
.
formatter
=
lggg
.
Formatter
(
fmt
=
_MESSAGE_FORMAT
,
datefmt
=
_DATE_FORMAT
)
self
.
formatter
=
lggg
.
Formatter
(
fmt
=
_MESSAGE_FORMAT
,
datefmt
=
_DATE_FORMAT
)
self
.
show_memory_usage
=
False
self
.
show_memory_usage
=
False
self
.
max_memory_usage
=
0
def
FormattedMessage
(
def
FormattedMessage
(
self
,
record
:
lggg
.
LogRecord
,
/
)
->
tuple
[
str
,
str
|
None
]:
self
,
record
:
lggg
.
LogRecord
,
/
)
->
tuple
[
str
,
list
[
str
]
|
None
]:
"""
"""
Note:
"
message
"
is not yet an attribute of record (it will be set by format()); Use
"
msg
"
instead.
Note:
"
message
"
is not yet an attribute of record (it will be set by format()); Use
"
msg
"
instead.
"""
"""
...
@@ -116,8 +117,16 @@ class _handler_extension:
...
@@ -116,8 +117,16 @@ class _handler_extension:
original_message
=
record
.
msg
original_message
=
record
.
msg
lines
=
original_message
.
splitlines
()
lines
=
original_message
.
splitlines
()
record
.
msg
=
lines
[
0
]
record
.
msg
=
lines
[
0
]
next_lines
=
_NEXT_LINE_PROLOGUE
.
join
(
lines
[
1
:])
next_lines
=
f
"
{
_NEXT_LINE_PROLOGUE
}{
next_lines
}
"
else
:
original_message
=
next_lines
=
None
record
.
elapsed_time
=
ElapsedTime
()
if
self
.
show_memory_usage
:
record
.
memory_usage
=
self
.
MemoryUsage
()
else
:
else
:
original_message
=
lines
=
None
record
.
memory_usage
=
""
formatted
=
self
.
formatter
.
format
(
record
)
formatted
=
self
.
formatter
.
format
(
record
)
...
@@ -125,16 +134,19 @@ class _handler_extension:
...
@@ -125,16 +134,19 @@ class _handler_extension:
if
original_message
is
not
None
:
if
original_message
is
not
None
:
record
.
msg
=
original_message
record
.
msg
=
original_message
return
formatted
,
lines
return
formatted
,
next_
lines
def
MemoryUsage
(
self
)
->
str
:
def
MemoryUsage
(
self
)
->
str
:
""""""
""""""
if
self
.
show_memory_usage
:
if
self
.
show_memory_usage
:
usage
=
_PROCESS
.
memory_info
().
rss
usage
=
_PROCESS
.
memory_info
().
rss
if
usage
>
_GIGA_SCALING
:
if
usage
>
self
.
max_memory_usage
:
return
f
"
:
{
round
(
_GIGA_SCALING
*
usage
,
1
)
}
GB
"
self
.
max_memory_usage
=
usage
if
usage
>
_GIGA_UNIT
:
return
f
"
:
{
round
(
usage
/
_GIGA_UNIT
,
1
)
}
GB
"
else
:
else
:
return
f
"
:
{
round
(
_MEGA_SCALING
*
usage
,
1
)
}
MB
"
return
f
"
:
{
round
(
usage
/
_MEGA_UNIT
,
1
)
}
MB
"
return
""
return
""
...
@@ -167,25 +179,24 @@ class console_handler_t(lggg.Handler, _handler_extension):
...
@@ -167,25 +179,24 @@ class console_handler_t(lggg.Handler, _handler_extension):
""""""
""""""
cls
=
self
.
__class__
cls
=
self
.
__class__
formatted
,
lines
=
self
.
FormattedMessage
(
record
)
formatted
,
next_
lines
=
self
.
FormattedMessage
(
record
)
highlighted
=
text_t
(
formatted
)
highlighted
=
text_t
(
formatted
)
highlighted
.
append
(
f
"
+
{
ElapsedTime
()
}{
self
.
MemoryUsage
()
}
"
,
style
=
"
green
"
)
if
lines
is
not
None
:
highlighted
.
append
(
_NEXT_LINE_PROLOGUE
+
_NEXT_LINE_PROLOGUE
.
join
(
lines
[
1
:])
)
highlighted
.
stylize
(
"
dodger_blue2
"
,
end
=
19
)
highlighted
.
stylize
(
"
dodger_blue2
"
,
end
=
19
)
highlighted
.
highlight_words
(
highlighted
.
highlight_words
(
(
f
"
[
{
record
.
levelname
}
]
"
,),
style
=
cls
.
_LEVEL_COLOR
[
record
.
levelno
]
(
f
"
[
{
record
.
levelname
}
]
"
,),
style
=
cls
.
_LEVEL_COLOR
[
record
.
levelno
]
)
)
highlighted
.
highlight_
regex
(
highlighted
.
highlight_
words
(
f
"
{
_WHERE_SEPARATOR
}
{
record
.
module
}
:
{
record
.
funcName
}
:
{
record
.
lineno
}
"
,
(
f
"
{
_WHERE_SEPARATOR
}
{
record
.
module
}
:
{
record
.
funcName
}
:
{
record
.
lineno
}
"
,
),
style
=
cls
.
_GRAY_STYLE
,
style
=
cls
.
_GRAY_STYLE
,
)
)
highlighted
.
highlight_regex
(
cls
.
_ACTUAL
,
style
=
"
red
"
)
highlighted
.
highlight_regex
(
f
"
\+
{
record
.
elapsed_time
}
.*$
"
,
style
=
"
green
"
)
highlighted
.
highlight_regex
(
cls
.
_EXPECTED
,
style
=
"
green
"
)
if
next_lines
is
not
None
:
highlighted
.
append
(
next_lines
)
highlighted
.
highlight_words
((
cls
.
_ACTUAL
,),
style
=
"
red
"
)
highlighted
.
highlight_words
((
cls
.
_EXPECTED
,),
style
=
"
green
"
)
self
.
console
.
print
(
highlighted
)
self
.
console
.
print
(
highlighted
)
...
@@ -200,14 +211,11 @@ class file_handler_t(lggg.FileHandler, _handler_extension):
...
@@ -200,14 +211,11 @@ class file_handler_t(lggg.FileHandler, _handler_extension):
def
emit
(
self
,
record
:
lggg
.
LogRecord
,
/
)
->
None
:
def
emit
(
self
,
record
:
lggg
.
LogRecord
,
/
)
->
None
:
""""""
""""""
formatted
,
lines
=
self
.
FormattedMessage
(
record
)
formatted
,
next_lines
=
self
.
FormattedMessage
(
record
)
formatted
=
formatted
.
replace
(
"
\t
"
,
(
9
-
record
.
levelname
.
__len__
())
*
"
"
)
if
lines
is
None
:
message
=
formatted
.
replace
(
"
\t
"
,
(
9
-
record
.
levelname
.
__len__
())
*
"
"
)
message
=
f
"
{
formatted
}
+
{
ElapsedTime
()
}{
self
.
MemoryUsage
()
}
"
if
next_lines
is
not
None
:
else
:
message
=
f
"
{
message
}{
next_lines
}
"
next_lines
=
_NEXT_LINE_PROLOGUE
.
join
(
lines
[
1
:])
message
=
f
"
{
formatted
}
+
{
ElapsedTime
()
}{
self
.
MemoryUsage
()
}{
_NEXT_LINE_PROLOGUE
}{
next_lines
}
"
print
(
message
,
file
=
self
.
stream
)
print
(
message
,
file
=
self
.
stream
)
self
.
stream
.
flush
()
self
.
stream
.
flush
()
...
@@ -315,3 +323,8 @@ def ElapsedTime() -> str:
...
@@ -315,3 +323,8 @@ def ElapsedTime() -> str:
output
=
output
.
split
(
maxsplit
=
1
)[
-
1
]
output
=
output
.
split
(
maxsplit
=
1
)[
-
1
]
return
output
return
output
def
MaximumMemoryUsage
()
->
int
:
""""""
return
max
(
_hdr
.
max_memory_usage
for
_hdr
in
LOGGER
.
handlers
)
This diff is collapsed.
Click to expand it.
logger_36/test.py
+
4
−
1
View file @
3559653c
...
@@ -32,7 +32,7 @@
...
@@ -32,7 +32,7 @@
from
pathlib
import
Path
as
path_t
from
pathlib
import
Path
as
path_t
from
tempfile
import
TemporaryDirectory
from
tempfile
import
TemporaryDirectory
from
logger_36
import
LOGGER
,
AddFileHandler
from
logger_36
import
LOGGER
,
AddFileHandler
,
MaximumMemoryUsage
,
SetShowMemoryUsage
with
TemporaryDirectory
()
as
tmp_folder
:
with
TemporaryDirectory
()
as
tmp_folder
:
...
@@ -40,11 +40,14 @@ with TemporaryDirectory() as tmp_folder:
...
@@ -40,11 +40,14 @@ with TemporaryDirectory() as tmp_folder:
tmp_file
=
tmp_folder
/
"
log.txt
"
tmp_file
=
tmp_folder
/
"
log.txt
"
AddFileHandler
(
tmp_file
)
AddFileHandler
(
tmp_file
)
SetShowMemoryUsage
(
True
)
for
level
in
(
"
debug
"
,
"
info
"
,
"
warning
"
,
"
error
"
,
"
critical
"
):
for
level
in
(
"
debug
"
,
"
info
"
,
"
warning
"
,
"
error
"
,
"
critical
"
):
LogMessage
=
getattr
(
LOGGER
,
level
)
LogMessage
=
getattr
(
LOGGER
,
level
)
LogMessage
(
f
"
{
level
.
capitalize
()
}
message
"
)
LogMessage
(
f
"
{
level
.
capitalize
()
}
message
"
)
LogMessage
(
f
"
Multi-line
\n
{
level
.
capitalize
()
}
\n
message
"
)
LogMessage
(
f
"
Multi-line
\n
{
level
.
capitalize
()
}
\n
message
"
)
LOGGER
.
info
(
f
"
Max memory usage:
{
MaximumMemoryUsage
()
}
"
)
content
=
open
(
tmp_file
,
"
r
"
).
read
()
content
=
open
(
tmp_file
,
"
r
"
).
read
()
print
(
f
"
\n
{
content
}
"
)
print
(
f
"
\n
{
content
}
"
)
This diff is collapsed.
Click to expand it.
logger_36/version.py
+
1
−
1
View file @
3559653c
...
@@ -29,4 +29,4 @@
...
@@ -29,4 +29,4 @@
# The fact that you are presently reading this means that you have had
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
# knowledge of the CeCILL license and that you accept its terms.
__version__
=
"
2023.
6
"
__version__
=
"
2023.
7
"
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