Logger
easy · levels, filtering, sink injection
Brief-reported interview attribution (unverified): Amazon, Microsoft
How to approach an LLD roundDesign a logging framework. Levels are DEBUG < INFO < WARN < ERROR.
The logger is created with a level; messages below that level are dropped.
log(level, message) formats a message as "[LEVEL] message". Output goes to a
sink callable passed to the constructor, or to an internal list (read via
messages()) when no sink is given. Invalid levels raise ValueError.
class Logger:
def __init__(self, level="INFO", sink=None):
# sink: callable taking one formatted string, or None
def set_level(self, level):
# raises ValueError for an unknown level
def log(self, level, message):
# raises ValueError for an unknown level; drops messages below the set level
def debug(self, message):
def info(self, message):
def warn(self, message):
def error(self, message):
def messages(self):
# -> list of formatted strings (empty when a sink is used)
Rules. The default level is "INFO". set_level and log with an unknown
level raise ValueError. A dropped message is never sent to the sink and never
stored.
Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.
Public demo
# Public demo: run after implementing the class API above.
l = Logger()
l.debug('dropped')
l.info('started')
l.warn('slow')
l.error('failed')
print(l.messages())Starter code
class Logger:
def __init__(self, level='INFO', sink=None):
"""sink: callable taking one formatted string, or None"""
pass
def set_level(self, level):
"""raises ValueError for an unknown level"""
pass
def log(self, level, message):
"""raises ValueError for an unknown level; drops messages below the set level"""
pass
def debug(self, message):
"""Implement the return value and rules in the API contract."""
pass
def info(self, message):
"""Implement the return value and rules in the API contract."""
pass
def warn(self, message):
"""Implement the return value and rules in the API contract."""
pass
def error(self, message):
"""Implement the return value and rules in the API contract."""
pass
def messages(self):
"""-> list of formatted strings (empty when a sink is used)"""
pass
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python compiler