summaryrefslogtreecommitdiff
path: root/azurelinuxagent/common/logger.py
diff options
context:
space:
mode:
authorƁukasz 'sil2100' Zemczak <lukasz.zemczak@ubuntu.com>2017-07-03 13:44:00 +0200
committerusd-importer <ubuntu-server@lists.ubuntu.com>2017-07-03 12:23:41 +0000
commit70c0ea1ac879b2e1cba0a8edb1f3fbe82652413b (patch)
treef168bb289117feb1c0d5b2c73604b44e85b064e2 /azurelinuxagent/common/logger.py
parent68754fe67f1b3da2e6ca45885641941b3229698a (diff)
parent63aafec1b1a480947da7d5579c3ffc66b8fe5bdb (diff)
downloadvyos-walinuxagent-70c0ea1ac879b2e1cba0a8edb1f3fbe82652413b.tar.gz
vyos-walinuxagent-70c0ea1ac879b2e1cba0a8edb1f3fbe82652413b.zip
Import patches-applied version 2.2.14-0ubuntu1 to applied/ubuntu/artful-proposed
Imported using git-ubuntu import. Changelog parent: 68754fe67f1b3da2e6ca45885641941b3229698a Unapplied parent: 63aafec1b1a480947da7d5579c3ffc66b8fe5bdb New changelog entries: * New upstream release (LP: #1701350). * debian/copyright: - Refreshed copyright content.
Diffstat (limited to 'azurelinuxagent/common/logger.py')
-rw-r--r--azurelinuxagent/common/logger.py35
1 files changed, 32 insertions, 3 deletions
diff --git a/azurelinuxagent/common/logger.py b/azurelinuxagent/common/logger.py
index c1eb18f..bfdc73a 100644
--- a/azurelinuxagent/common/logger.py
+++ b/azurelinuxagent/common/logger.py
@@ -20,7 +20,13 @@ Log utils
import os
import sys
from azurelinuxagent.common.future import ustr
-from datetime import datetime
+from datetime import datetime, timedelta
+
+EVERY_DAY = timedelta(days=1)
+EVERY_HALF_DAY = timedelta(hours=12)
+EVERY_HOUR = timedelta(hours=1)
+EVERY_HALF_HOUR = timedelta(minutes=30)
+EVERY_FIFTEEN_MINUTES = timedelta(minutes=15)
class Logger(object):
"""
@@ -28,10 +34,23 @@ class Logger(object):
"""
def __init__(self, logger=None, prefix=None):
self.appenders = []
- if logger is not None:
- self.appenders.extend(logger.appenders)
+ self.logger = self if logger is None else logger
+ self.periodic_messages = {}
self.prefix = prefix
+ def reset_periodic(self):
+ self.logger.periodic_messages = {}
+
+ def is_period_elapsed(self, delta, h):
+ return h not in self.logger.periodic_messages or \
+ (self.logger.periodic_messages[h] + delta) <= datetime.now()
+
+ def periodic(self, delta, msg_format, *args):
+ h = hash(msg_format)
+ if self.is_period_elapsed(delta, h):
+ self.info(msg_format, *args)
+ self.logger.periodic_messages[h] = datetime.now()
+
def verbose(self, msg_format, *args):
self.log(LogLevel.VERBOSE, msg_format, *args)
@@ -62,8 +81,12 @@ class Logger(object):
log_item = ustr(log_item.encode('ascii', "backslashreplace"),
encoding="ascii")
+
for appender in self.appenders:
appender.write(level, log_item)
+ if self.logger != self:
+ for appender in self.logger.appenders:
+ appender.write(level, log_item)
def add_appender(self, appender_type, level, path):
appender = _create_logger_appender(appender_type, level, path)
@@ -129,6 +152,12 @@ class AppenderType(object):
def add_logger_appender(appender_type, level=LogLevel.INFO, path=None):
DEFAULT_LOGGER.add_appender(appender_type, level, path)
+def reset_periodic():
+ DEFAULT_LOGGER.reset_periodic()
+
+def periodic(delta, msg_format, *args):
+ DEFAULT_LOGGER.periodic(delta, msg_format, *args)
+
def verbose(msg_format, *args):
DEFAULT_LOGGER.verbose(msg_format, *args)