-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: richardyu-ms <richard.yu@microsoft.com>
- Loading branch information
richardyu
authored and
root
committed
Nov 14, 2022
1 parent
36c235c
commit f7c40db
Showing
12 changed files
with
420 additions
and
6 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
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
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,58 @@ | ||
import logging | ||
import os | ||
import sys | ||
from logging.handlers import RotatingFileHandler | ||
|
||
ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
LOG_FILE_DIR = os.path.join(ROOT_DIR, 'logs') | ||
LOG_FILE_PATH = os.path.join(LOG_FILE_DIR, 'output.log') | ||
|
||
global logger | ||
logger = logging.getLogger() | ||
|
||
|
||
class InfoFilter(logging.Filter): | ||
def filter(self, rec): | ||
return rec.levelno in (logging.DEBUG, logging.INFO, logging.WARN) | ||
|
||
def set_logging(): | ||
# Create a custom logger | ||
|
||
logger.setLevel(level=logging.DEBUG) | ||
|
||
# Create handlers | ||
console_debug_handler = logging.StreamHandler(sys.stdout) | ||
console_error_handler = logging.StreamHandler() | ||
if not os.path.exists(LOG_FILE_DIR): | ||
os.mkdir(LOG_FILE_DIR) | ||
|
||
file_handler = RotatingFileHandler(LOG_FILE_PATH, maxBytes=20000000, backupCount=5) | ||
|
||
if os.path.exists(LOG_FILE_PATH): | ||
'''rollover on each run''' | ||
file_handler.doRollover() | ||
|
||
|
||
console_debug_handler.setLevel(logging.INFO) | ||
console_debug_handler.addFilter(InfoFilter()) | ||
file_handler.setLevel(logging.INFO) | ||
console_error_handler.setLevel(logging.ERROR) | ||
|
||
# Create formatters and add it to handlers | ||
c_debug_format = logging.Formatter(fmt='%(name)s - %(filename)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') | ||
c_error_format = logging.Formatter(fmt='%(filename)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') | ||
f_format = logging.Formatter(fmt='%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(message)s', | ||
datefmt='%m/%d/%Y %I:%M:%S %p') | ||
|
||
console_error_handler.setFormatter(c_error_format) | ||
console_debug_handler.setFormatter(c_debug_format) | ||
file_handler.setFormatter(f_format) | ||
|
||
# Add handlers to the logger | ||
logger.addHandler(console_debug_handler) | ||
logger.addHandler(file_handler) | ||
logger.addHandler(console_error_handler) | ||
|
||
|
||
def rollover_log(): | ||
file_handler.doRollover() |
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,28 @@ | ||
|
||
import unittest | ||
import inspect | ||
|
||
from LogConfig import logger | ||
import LogConfig | ||
|
||
|
||
|
||
class BasicMockedTest(unittest.TestCase): | ||
"""Basic test case, use to simualate the sai_base_test class | ||
""" | ||
|
||
def setUp(self): | ||
unittest.TestCase.setUp(self) | ||
self.set_logger_name() | ||
|
||
|
||
def set_logger_name(self): | ||
""" | ||
Set Logger name as filename:classname | ||
""" | ||
LogConfig.set_logging() | ||
file_name = inspect.getfile(self.__class__) | ||
class_name = self.__class__.__name__ | ||
logger.name = "{}:{}".format(file_name, class_name) | ||
|
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,58 @@ | ||
import logging | ||
import os | ||
import sys | ||
from logging.handlers import RotatingFileHandler | ||
|
||
ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
LOG_FILE_DIR = os.path.join(ROOT_DIR, 'logs') | ||
LOG_FILE_PATH = os.path.join(LOG_FILE_DIR, 'output.log') | ||
|
||
global logger | ||
logger = logging.getLogger() | ||
|
||
|
||
class InfoFilter(logging.Filter): | ||
def filter(self, rec): | ||
return rec.levelno in (logging.DEBUG, logging.INFO, logging.WARN) | ||
|
||
def set_logging(): | ||
# Create a custom logger | ||
|
||
logger.setLevel(level=logging.DEBUG) | ||
|
||
# Create handlers | ||
console_debug_handler = logging.StreamHandler(sys.stdout) | ||
console_error_handler = logging.StreamHandler() | ||
if not os.path.exists(LOG_FILE_DIR): | ||
os.mkdir(LOG_FILE_DIR) | ||
|
||
file_handler = RotatingFileHandler(LOG_FILE_PATH, maxBytes=20000000, backupCount=5) | ||
|
||
if os.path.exists(LOG_FILE_PATH): | ||
'''rollover on each run''' | ||
file_handler.doRollover() | ||
|
||
|
||
console_debug_handler.setLevel(logging.INFO) | ||
console_debug_handler.addFilter(InfoFilter()) | ||
file_handler.setLevel(logging.INFO) | ||
console_error_handler.setLevel(logging.ERROR) | ||
|
||
# Create formatters and add it to handlers | ||
c_debug_format = logging.Formatter(fmt='%(name)s - %(filename)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') | ||
c_error_format = logging.Formatter(fmt='%(filename)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') | ||
f_format = logging.Formatter(fmt='%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(message)s', | ||
datefmt='%m/%d/%Y %I:%M:%S %p') | ||
|
||
console_error_handler.setFormatter(c_error_format) | ||
console_debug_handler.setFormatter(c_debug_format) | ||
file_handler.setFormatter(f_format) | ||
|
||
# Add handlers to the logger | ||
logger.addHandler(console_debug_handler) | ||
logger.addHandler(file_handler) | ||
logger.addHandler(console_error_handler) | ||
|
||
|
||
def rollover_log(): | ||
file_handler.doRollover() |
Oops, something went wrong.