-
Notifications
You must be signed in to change notification settings - Fork 858
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- log.py -> cloudinit/log/loggers.py - move logging helpers from util.py into cloudinit/log/log_util.py - expose all of these functions in __init__.py
- Loading branch information
Showing
27 changed files
with
216 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file is part of cloud-init. See LICENSE file for license information. | ||
|
||
from cloudinit.log.log_util import error, log_time, logexc, multi_log | ||
from cloudinit.log.loggers import ( | ||
DEPRECATED, | ||
TRACE, | ||
CustomLoggerType, | ||
LogExporter, | ||
configure_root_logger, | ||
flush_loggers, | ||
reset_logging, | ||
setup_basic_logging, | ||
setup_logging, | ||
) | ||
|
||
__all__ = [ | ||
"CustomLoggerType", | ||
"DEPRECATED", | ||
"LogExporter", | ||
"TRACE", | ||
"configure_root_logger", | ||
"error", | ||
"flush_loggers", | ||
"logexc", | ||
"log_time", | ||
"multi_log", | ||
"reset_logging", | ||
"setup_basic_logging", | ||
"setup_logging", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import logging | ||
import os | ||
import sys | ||
import time | ||
|
||
|
||
def logexc( | ||
log, msg, *args, log_level: int = logging.WARNING, exc_info=True | ||
) -> None: | ||
log.log(log_level, msg, *args) | ||
log.debug(msg, exc_info=exc_info, *args) | ||
|
||
|
||
class log_time: | ||
""" | ||
Log time that it takes for context to run if the time exceeds a threshold. | ||
:param msg: Name of the thing that is being measured | ||
:param threshold: Threashold, in seconds. When the context exceeds this | ||
threshold, a log will be made. | ||
:param skip_log: Do not log a message. If a message would have been | ||
logged it can be accessed from the context manager's 'output' variable | ||
This is useful for consolidating the message with other logs. | ||
usage: | ||
this call: | ||
``` | ||
with log_time("Configuring the network"): | ||
run_configure() | ||
``` | ||
might produce this log: | ||
``` | ||
Configuring the network took 0.02 seconds | ||
``` | ||
""" | ||
|
||
def __init__(self, msg, threshold: float = 0.01, skip_log: bool = False): | ||
self._msg = msg | ||
self._threshold = threshold | ||
self._skip_log = skip_log | ||
self.output = "" | ||
self.start = 0.0 | ||
|
||
def __enter__(self): | ||
self.start = time.monotonic() | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
delta = time.monotonic() - self.start | ||
if delta > self._threshold: | ||
if not self._skip_log: | ||
LOG.debug("%s took %0.3f seconds", self._msg, delta) | ||
self.output = f"{self._msg} took {delta:.3f} seconds" | ||
|
||
|
||
def write_to_console(conpath, text): | ||
with open(conpath, "w") as wfh: | ||
wfh.write(text) | ||
wfh.flush() | ||
|
||
|
||
def multi_log( | ||
text, | ||
console=True, | ||
stderr=True, | ||
log=None, | ||
log_level=logging.DEBUG, | ||
fallback_to_stdout=True, | ||
): | ||
if stderr: | ||
sys.stderr.write(text) | ||
if console: | ||
conpath = "/dev/console" | ||
writing_to_console_worked = False | ||
if os.path.exists(conpath): | ||
try: | ||
write_to_console(conpath, text) | ||
writing_to_console_worked = True | ||
except OSError: | ||
console_error = "Failed to write to /dev/console" | ||
sys.stdout.write(f"{console_error}\n") | ||
if log: | ||
log.log(logging.WARNING, console_error) | ||
|
||
if fallback_to_stdout and not writing_to_console_worked: | ||
# A container may lack /dev/console (arguably a container bug). | ||
# Additionally, /dev/console may not be writable to on a VM (again | ||
# likely a VM bug or virtualization bug). | ||
# | ||
# If either of these is the case, then write output to stdout. | ||
# This will result in duplicate stderr and stdout messages if | ||
# stderr was True. | ||
# | ||
# even though systemd might have set up output to go to | ||
# /dev/console, the user may have configured elsewhere via | ||
# cloud-config 'output'. If there is /dev/console, messages will | ||
# still get there. | ||
sys.stdout.write(text) | ||
if log: | ||
if text[-1] == "\n": | ||
log.log(log_level, text[:-1]) | ||
else: | ||
log.log(log_level, text) | ||
|
||
|
||
def error(msg, rc=1, fmt="Error:\n{}", sys_exit=False): | ||
r"""Print error to stderr and return or exit | ||
@param msg: message to print | ||
@param rc: return code (default: 1) | ||
@param fmt: format string for putting message in (default: 'Error:\n {}') | ||
@param sys_exit: exit when called (default: false) | ||
""" | ||
print(fmt.format(msg), file=sys.stderr) | ||
if sys_exit: | ||
sys.exit(rc) | ||
return rc |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.