Skip to content

Commit

Permalink
[Core] Factor out logger
Browse files Browse the repository at this point in the history
  • Loading branch information
m4yers committed Sep 6, 2017
1 parent 6956ef2 commit 6f48d2b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
11 changes: 7 additions & 4 deletions crutch/core/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def add_hook_before(self, phase, hook):
def add_hook_after(self, phase, hook):
self.add_hook(phase, ORDER_AFTER, hook)

def mark(self, phase, order=ORDER_NONE, info=''):
def mark(self, phase, order=ORDER_NONE, info=None):
if self.tracing:
LOGGER.info('LIFECYCLE: {} {} {}'.format(order, phase, info))

Expand All @@ -83,10 +83,13 @@ def mark(self, phase, order=ORDER_NONE, info=''):
return

for hook in orders[order]:
hook(info)
if info:
hook(info)
else:
hook()

def mark_before(self, phase, info=''):
def mark_before(self, phase, info=None):
self.mark(phase, ORDER_BEFORE, info)

def mark_after(self, phase, info=''):
def mark_after(self, phase, info=None):
self.mark(phase, ORDER_AFTER, info)
73 changes: 73 additions & 0 deletions crutch/core/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-

# Copyright © 2017 Artyom Goncharov
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from __future__ import unicode_literals
from __future__ import print_function

import logging

import crutch.core.lifecycle as Lifecycle


class Logging(object):

def __init__(self, renv):
self.renv = renv
self.logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.setLevel(logging.WARNING)

self.table = {
'critical': logging.CRITICAL,
'error': logging.ERROR,
'warning': logging.WARNING,
'info': logging.INFO,
'debug': logging.DEBUG
}

self.renv.lifecycle.add_hook_before(
Lifecycle.CONFIG_FLUSH, self.update_config)
self.renv.lifecycle.add_hook_after(
Lifecycle.CONFIG_LOAD, self.update_level)

def update_config(self):
self.renv.set_prop(
'crutch_logging_level',
logging.getLevelName(self.logger.getEffectiveLevel()).lower(),
mirror_to_config=True)

def update_level(self):
level = self.renv.props.config.get('crutch_logging_level', None)
if not level:
return

lowered = level.lower()
if lowered not in self.table:
self.logger.error("Unknown logging error %s", level)
return

self.logger.setLevel(self.table[lowered])

18 changes: 4 additions & 14 deletions crutch/core/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
from __future__ import unicode_literals
from __future__ import print_function

import logging

from crutch.core.log import Logging
from crutch.core.properties import Properties
from crutch.core.replacements import GenerativeReplacementsProvider
from crutch.core.replacements import Replacements
Expand Down Expand Up @@ -57,7 +56,7 @@ class RuntimeEnvironment(object):
Object of this class holds all necessary utility to get runner run
"""

root_logger = None
logger = None
Default = None

def __init__(self, runners):
Expand All @@ -72,17 +71,8 @@ def __init__(self, runners):
'prop-to-repl-mirror', dict())
self.repl.add_provider('runtime-env-repl', RuntimeEnvReplProvider(self))

# Init root logger
if not RuntimeEnvironment.root_logger:
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.WARNING)
# logger.setLevel(logging.DEBUG)
RuntimeEnvironment.root_logger = logger
if not RuntimeEnvironment.logger:
RuntimeEnvironment.logger = Logging(self)

@staticmethod
def get_default():
Expand Down

0 comments on commit 6f48d2b

Please sign in to comment.