Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Docker-in-Docker (DinD) capabilities of public runners deactivated.
More info
Open sidebar
JOSSOUD Olivier
WIM Collect
Commits
527fa4bb
Commit
527fa4bb
authored
Jan 21, 2020
by
JOSSOUD Olivier
Browse files
Debug Logger.
parent
5612cdb0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
0 deletions
+43
-0
wimcollect/logger.py
wimcollect/logger.py
+43
-0
No files found.
wimcollect/logger.py
0 → 100644
View file @
527fa4bb
import
sys
import
configobj
import
datetime
import
os
class
Logger
:
def
__init__
(
self
,
config_parser
:
configobj
.
ConfigObj
):
self
.
config
=
config_parser
self
.
log_dir
=
self
.
config
[
"LOGGER"
][
"log_dir"
]
self
.
__create_log_dir__
()
self
.
__delete_old_files__
()
def
write
(
self
,
instrument
:
str
,
message
:
str
)
->
None
:
now
=
datetime
.
datetime
.
now
(
tz
=
datetime
.
timezone
.
utc
)
now_str
=
now
.
strftime
(
"%Y-%m-%d %H:%M:%S"
)
msg
=
"["
+
now_str
+
"]"
if
instrument
!=
""
:
msg
+=
"["
+
instrument
+
"]"
msg
+=
" "
+
message
+
"
\n
"
sys
.
stdout
.
write
(
msg
)
with
open
(
self
.
__get_current_file_path__
(
now
),
"a"
)
as
log_file
:
log_file
.
write
(
msg
)
def
__get_current_file_path__
(
self
,
now
:
datetime
.
datetime
)
->
str
:
file_name
=
now
.
strftime
(
"%Y-%m-%d"
)
+
".log"
file_path
=
os
.
path
.
join
(
self
.
log_dir
,
file_name
)
return
file_path
def
__delete_old_files__
(
self
)
->
None
:
log_files
=
os
.
listdir
(
self
.
log_dir
)
for
log_file
in
log_files
:
full_file_path
=
os
.
path
.
join
(
self
.
log_dir
,
log_file
)
file_stats
=
os
.
stat
(
full_file_path
)
modification_time
=
datetime
.
datetime
.
fromtimestamp
(
file_stats
.
st_mtime
)
if
(
datetime
.
datetime
.
now
()
-
modification_time
).
days
>
int
(
self
.
config
[
"LOGGER"
][
"keep_log_days"
]):
os
.
remove
(
full_file_path
)
def
__create_log_dir__
(
self
)
->
None
:
if
not
os
.
path
.
exists
(
self
.
log_dir
):
os
.
mkdir
(
self
.
log_dir
)
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