Skip to content

Commit

Permalink
feat: override site name via _site
Browse files Browse the repository at this point in the history
added filter parameter for logger
  • Loading branch information
gavindsouza committed Jul 27, 2020
1 parent c87d598 commit ad1152f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions frappe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1559,10 +1559,10 @@ def _get_doctype_app():

loggers = {}
log_level = None
def logger(module=None, with_more_info=False, _site=None):
def logger(module=None, with_more_info=False, _site=None, filter=None):
'''Returns a python logger that uses StreamHandler'''
from frappe.utils.logger import get_logger
return get_logger(module=module, with_more_info=with_more_info, _site=_site)
return get_logger(module=module, with_more_info=with_more_info, _site=_site, filter=filter)

def log_error(message=None, title=_("Error")):
'''Log error to Error Log'''
Expand Down
16 changes: 9 additions & 7 deletions frappe/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@
site = getattr(frappe.local, 'site', None)


def get_logger(module, with_more_info=False, _site=None):
def get_logger(module, with_more_info=False, _site=None, filter=None):
"""Application Logger for your given module
Args:
module (str): Name of your logger and consequently your log file.
with_more_info (bool, optional): Will log the form dict using the SiteContextFilter. Defaults to False.
_site (str, optional): If set, validates the current site context with the passed value. The `frappe.web` logger uses this to determine that the application is logging information related to the logger called. Defaults to None.
_site (str, optional): Overrides the global site variable set in this scope. Defaults to None.
filter (function, optional): Add a filter function for your logger
Returns:
<class 'logging.Logger'>: Returns a Python logger object with Site and Bench level logging capabilities.
"""
global site

def allow_site():
return (_site and _site == site) or bool(site)

if module in frappe.loggers:
return frappe.loggers[module]

Expand All @@ -42,6 +40,7 @@ def allow_site():

logfile = module + '.log'
site = getattr(frappe.local, 'site', None)
equivalent_site = _site or site
LOG_FILENAME = os.path.join('..', 'logs', logfile)

logger = logging.getLogger(module)
Expand All @@ -52,15 +51,18 @@ def allow_site():
handler = RotatingFileHandler(LOG_FILENAME, maxBytes=100_000, backupCount=20)
logger.addHandler(handler)

if allow_site():
SITELOG_FILENAME = os.path.join(site, 'logs', logfile)
if equivalent_site:
SITELOG_FILENAME = os.path.join(equivalent_site, 'logs', logfile)
site_handler = RotatingFileHandler(SITELOG_FILENAME, maxBytes=100_000, backupCount=20)
site_handler.setFormatter(formatter)
logger.addHandler(site_handler)

if with_more_info:
handler.addFilter(SiteContextFilter())

if filter:
logger.addFilter(filter)

handler.setFormatter(formatter)

frappe.loggers[module] = logger
Expand Down

0 comments on commit ad1152f

Please sign in to comment.