Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mpsd-software-manager
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
MPSD Computational Science
mpsd-software-manager
Commits
2bdf0df3
Commit
2bdf0df3
authored
1 year ago
by
Ashwin Kumar Karnad
Browse files
Options
Downloads
Plain Diff
Merge remote-tracking branch 'origin/main' into restructure-log-location
parents
4bf382d0
abfe87c3
No related branches found
No related tags found
1 merge request
!29
Restructure log location
Pipeline
#370049
passed
1 year ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mpsd-software-environment.py
+105
-16
105 additions, 16 deletions
mpsd-software-environment.py
with
105 additions
and
16 deletions
mpsd-software-environment.py
+
105
−
16
View file @
2bdf0df3
...
...
@@ -156,7 +156,7 @@ def set_up_logging(loglevel="warning", filename=None):
This function sets up the logging configuration for the script.
It configures the log level, log format, and log handlers
for both file and console output.
for both file and console
(=shell)
output.
Parameters
...
...
@@ -170,34 +170,121 @@ def set_up_logging(loglevel="warning", filename=None):
- filename to save logging messages into
If loglevel is
'
debug
'
, save line numbers in log messages.
Returns
-------
None.
Logger instances are generally not passed around, but retrieved from the
logging module as shown below (they are singletons).
We provide two loggers:
1. log = logging.getLogger(
''
)
This is the
'
root
'
logger. It uses a RichHandler if rich is available for
output to the shell, otherwise plain text.
Typical use:
log.debug(
"
...
"
)
log.info(
"
...
"
)
log.warn(
"
...
"
)
Equivalent to
logging.debug(
"
...
"
)
logging.info(
"
...
"
)
2. print_log = logging.getlogger(
'
print
'
)
This uses the logging module to issue the message, but prints without
any further markup (i.e. no date, loglevel, line number, etc). Think
PRINT via the LOGging module.
We use this as a replacement for the print function (i.e. for messages
that should not be affected by logging levels, and which should always
be printed).
Typical and intended use:
print_log.info(
"
Available toolchains are ...
"
)
The major difference from the normal print command is that the output
will be send to the stdout (as for print) AND the file with name
filename, so that these messages appear in the log file together with
normal log output.
"""
# convert loglevel string into loglevel as number
log_level_numeric
=
getattr
(
logging
,
loglevel
.
upper
(),
logging
.
WARNING
)
assert
log_level_numeric
if
not
isinstance
(
log_level_numeric
,
int
):
raise
ValueError
(
"
Invalid log level: %s
"
%
loglevel
)
handlers
=
[]
if
filename
:
handlers
.
append
(
logging
.
FileHandler
(
filename
))
# set up the main logger ("root" logger)
logger
=
logging
.
getLogger
(
""
)
# - "logger" logs everything
# - we use loglevel at handler level to write everything to file
# - and filter using log_level_numeric (as the user provides) to
# send logging messages to the console
logger
.
setLevel
(
0
)
# the handler determines where the logs go: stdout/file
if
rich_available
:
# set up logging as recommended for rich, see
# https://rich.readthedocs.io/en/stable/logging.html
handlers
.
append
(
rich
.
logging
.
RichHandler
())
shell_handler
=
rich
.
logging
.
RichHandler
()
# rich handler provides metadata automatically:
logging_format
=
"
%(message)s
"
else
:
# rich not available, define our own output
# for shell output, only show time (not date and time)
shell_formatter
=
logging
.
Formatter
(
logging_format
,
datefmt
=
"
[%X]
"
)
else
:
shell_handler
=
logging
.
StreamHandler
()
# include line numbers in output if level is DEBUG
linenumbers
=
"
%(lineno)4d
"
if
log_level_numeric
==
logging
.
DEBUG
else
""
handlers
.
append
(
logging
.
StreamHandler
())
logging_format
=
"
%(asctime)s %(levelname)7s
"
+
linenumbers
+
"
| %(message)s
"
shell_formatter
=
logging
.
Formatter
(
logging_format
)
logging
.
basicConfig
(
level
=
log_level_numeric
,
format
=
logging_format
,
datefmt
=
"
[%X]
"
,
handlers
=
handlers
,
force
=
True
,
)
# here we hook everything together
shell_handler
.
setFormatter
(
shell_formatter
)
# use the log_level_numeric to decide how much logging is sent to shell
shell_handler
.
setLevel
(
log_level_numeric
)
logger
.
addHandler
(
shell_handler
)
# if filename provided, write log messages to that file, too.
if
filename
:
file_handler
=
logging
.
FileHandler
(
filename
)
# if we have a file, we write all information in there.
# We could change the level, for example restrict to only DEBUG and above with
# file_handler.setLevel(logging.DEBUG)
file_logging_format
=
"
%(asctime)s %(levelname)7s %(lineno)4d | %(message)s
"
file_formatter
=
logging
.
Formatter
(
file_logging_format
,
datefmt
=
"
[%X]
"
)
file_handler
.
setFormatter
(
file_formatter
)
logger
.
addHandler
(
file_handler
)
#
# new logger for printing
#
print_log
=
logging
.
getLogger
(
"
print
"
)
print_log
.
setLevel
(
logging
.
INFO
)
print_log
.
propagate
=
False
# create formatter 'empty' formatter
formatter
=
logging
.
Formatter
(
"
%(message)s
"
)
# create, format and add handler for shell output
ch
=
logging
.
StreamHandler
()
ch
.
setFormatter
(
formatter
)
print_log
.
addHandler
(
ch
)
# if filename provided, write output of print_log to that file, too
if
filename
:
# create, format and add file handler
fh
=
logging
.
FileHandler
(
filename
)
fh
.
setFormatter
(
formatter
)
print_log
.
addHandler
(
fh
)
#
# short message
#
logging
.
debug
(
f
"
Logging has been setup, loglevel=
{
loglevel
.
upper
()
}
"
+
f
"
{
filename
=
}
{
rich_available
=
}
"
...
...
@@ -651,6 +738,8 @@ def install_environment(
"
No toolchains requested. Available toolchains for release
"
f
"
{
mpsd_release
}
are:
\n
{
available_toolchains
}
"
)
print_log
=
logging
.
getLogger
(
"
print
"
)
print_log
.
info
(
f
"
{
available_toolchains
=
}
"
)
return
for
toolchain
in
toolchains
:
...
...
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