-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
INFO messages should go to stdout #43
Comments
I am unsure how to change it so only INFO-level messages go to stdout and the rest go to stderr. I could replace all of the If anyone knows of a way to redirect log messages to stderr by log-level, I would appreciate the help. |
Perhaps you could have the basic setup log to def _init_logging(self) -> None:
"""
Initializes the logging parameters.
"""
today = datetime.datetime.now()
log_file = self.log_dir / f"{today:%Y-%m-%d}.log"
if not self.log_dir.exists():
# Make a new log directory
os.makedirs(log_file.parent)
else:
# Log dir already exists, lets check if we need to prune old logs
logs = self.log_dir.glob('*.log')
for log in logs:
log_date_str = str(log.stem)
log_date = datetime.datetime.strptime(log_date_str, "%Y-%m-%d")
if log_date + datetime.timedelta(days=self.config["# logs to keep"]) < today:
# Log is too old, delete!
os.remove(str(log))
stderr_level = logging.WARNING
stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setLevel(stderr_level)
class FilterNotStdErr(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
return record.levelno < stderr_level
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.addFilter(FilterNotStdErr())
logging.basicConfig(
level=logging.DEBUG if self.verbose else logging.INFO,
format="%(asctime)s - %(levelname)s: %(message)s",
datefmt="%Y-%m-%d %I:%M:%S %p",
force=True, # an import might already have the root logger configured
handlers=[
stderr_handler,
stdout_handler,
logging.FileHandler(log_file),
],
)
self.logger = logging.getLogger(f"cvdupdate-{self.version}")
# Also set the log level for urllib3, because it's "DEBUG" by default,
# and we may not like that.
urllib3_logger = logging.getLogger("urllib3.connectionpool")
urllib3_logger.setLevel(self.logger.level) |
@micahsnyder any chance this could be merged? I'd like to not have to send stderr to /dev/null in case something goes awry with an update. But as it is, running it as a cron would be very noisy. |
Hi @claviola thank you for the reminder. I'll try to review this suggestion next week. If you want to submit a PR for the suggested change, that does make things easier. |
Any chance this could be reviewed and merged? |
I just tested the proposed change. I added this to the end of the init function to test each level, since otherwise WARNING and ERROR won't be easily tested: # test logging to each of debug, info, warning, and error
self.logger.debug("Debug log message")
self.logger.info("Info log message")
self.logger.warning("Warning log message")
self.logger.error("Error log message") I then ran: cvd update --verbose 2> error.log 1> output.log I aborted the update with Ctrl-C after a few seconds since that's enough logs to see what is going on. What I found is that WARNING and ERROR do go to stderr and INFO goes to stdout, but also DEBUG goes to stdout.
I'm okay with this. I think some folks prefer debug to go to stderr. But realistically most people won't use the debug/verbose option. |
Currently all log messages are going to stderr. Fixes: #43 Patch courtesy of GitHub user backbord.
Current info messages appear to go to stderr:
This makes it hard to capture errors from cron jobs.
The text was updated successfully, but these errors were encountered: