-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
125 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
import logging | ||
|
||
import os | ||
from pythonjsonlogger import jsonlogger | ||
|
||
BARTERDUDE_DEFAULT_LOG_NAME = os.environ.get( | ||
"BARTERDUDE_DEFAULT_LOG_NAME", "barterdude" | ||
) | ||
BARTERDUDE_DEFAULT_LOG_LEVEL = int( | ||
os.environ.get( | ||
"BARTERDUDE_DEFAULT_LOG_LEVEL", logging.INFO | ||
) | ||
) | ||
|
||
handler = logging.StreamHandler() | ||
handler.setFormatter(jsonlogger.JsonFormatter( | ||
'(levelname) (name) (pathname) (lineno)', | ||
timestamp=True | ||
)) | ||
logger = logging.getLogger('barterdude') | ||
logger.addHandler(handler) | ||
|
||
|
||
def getLogger( | ||
name=BARTERDUDE_DEFAULT_LOG_NAME, | ||
level=BARTERDUDE_DEFAULT_LOG_LEVEL | ||
): | ||
logger = logging.getLogger(name) | ||
logger.setLevel(level) | ||
logger.addHandler(handler) | ||
return logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import logging | ||
from pythonjsonlogger import jsonlogger | ||
|
||
from asynctest import TestCase | ||
from barterdude.conf import ( | ||
getLogger, BARTERDUDE_DEFAULT_LOG_NAME, BARTERDUDE_DEFAULT_LOG_LEVEL | ||
) | ||
|
||
|
||
class TestConf(TestCase): | ||
|
||
def setUp(self): | ||
self.log_name = BARTERDUDE_DEFAULT_LOG_NAME | ||
self.log_level = BARTERDUDE_DEFAULT_LOG_LEVEL | ||
|
||
async def test_should_get_log_with_default_configs(self): | ||
logger = getLogger() | ||
self.assertEqual( | ||
type(logger.handlers[0]), | ||
logging.StreamHandler | ||
) | ||
self.assertEqual(logger.name, self.log_name) | ||
self.assertEqual(logger.level, self.log_level) | ||
self.assertEqual( | ||
type(logger.handlers[0].formatter), | ||
jsonlogger.JsonFormatter | ||
) | ||
|
||
async def test_should_get_log_with_custom_configs(self): | ||
logger = getLogger("test_log", logging.DEBUG) | ||
self.assertEqual( | ||
type(logger.handlers[0]), | ||
logging.StreamHandler | ||
) | ||
self.assertEqual(logger.name, "test_log") | ||
self.assertEqual(logger.level, logging.DEBUG) | ||
self.assertEqual( | ||
type(logger.handlers[0].formatter), | ||
jsonlogger.JsonFormatter | ||
) | ||
|
||
async def test_should_get_log_with_custom_configs_even_called_after(self): | ||
logger_first = getLogger() | ||
logger = getLogger("test_log", logging.DEBUG) | ||
logger_last = getLogger() | ||
self.assertEqual( | ||
type(logger.handlers[0]), | ||
logging.StreamHandler | ||
) | ||
self.assertEqual(logger_first.name, self.log_name) | ||
self.assertEqual(logger_first.level, self.log_level) | ||
self.assertEqual(logger.name, "test_log") | ||
self.assertEqual(logger.level, logging.DEBUG) | ||
self.assertEqual(logger_last.name, self.log_name) | ||
self.assertEqual(logger_last.level, self.log_level) | ||
self.assertEqual( | ||
type(logger.handlers[0].formatter), | ||
jsonlogger.JsonFormatter | ||
) | ||
self.assertEqual(len(logger_first.handlers), 1) | ||
self.assertEqual(len(logger.handlers), 1) | ||
self.assertEqual(len(logger_last.handlers), 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters