Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
Signed-off-by: richardyu-ms <richard.yu@microsoft.com>
  • Loading branch information
richardyu-ms committed Nov 15, 2022
1 parent 419adcc commit 9723951
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 128 deletions.
63 changes: 25 additions & 38 deletions ptf/LogConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,39 @@
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()

def set_logging(loggerName='main'):
"""Set the logger.
"""

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 formatters and add it to handlers
c_debug_format = '%(name)s - %(filename)s - %(levelname)s - %(message)s'
c_error_format = '%(filename)s - %(levelname)s - %(message)s'
f_format = '%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(message)s'
datafmt = '%m/%d/%Y %I:%M:%S %p'

# 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')
# Create a custom logger
logger = logging.getLogger()
logger.name = loggerName
logger.setLevel(logging.INFO)

console_error_handler.setFormatter(c_error_format)
console_debug_handler.setFormatter(c_debug_format)
file_handler.setFormatter(f_format)
file_formatter = logging.Formatter(f_format, datafmt)

# Add handlers to the logger
logger.addHandler(console_debug_handler)
# Create handlers
# Add a new file handler
file_handler = logging.FileHandler(LOG_FILE_PATH)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
logger.addHandler(console_error_handler)


def rollover_log():
file_handler.doRollover()
# Add a new stream handler
console_formatter = logging.Formatter(
c_error_format, datafmt)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.ERROR)
stream_handler.setFormatter(console_formatter)
logger.addHandler(stream_handler)
6 changes: 3 additions & 3 deletions ptf/sai_base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from thrift.protocol import TBinaryProtocol

from sai_thrift import sai_rpc
from LogConfig import logger
import LogConfig

from sai_utils import *
Expand Down Expand Up @@ -163,10 +162,11 @@ 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)
logger_name = "{}:{}".format(file_name, class_name)
LogConfig.set_logging(loggerName = logger_name)


def get_active_port_list(self):
Expand Down
12 changes: 6 additions & 6 deletions ptf/utest/BasicTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import unittest
import inspect

from LogConfig import logger
import LogConfig


"""
Base class of the unit test
"""

class BasicMockedTest(unittest.TestCase):
"""Basic test case, use to simualate the sai_base_test class
Expand All @@ -14,14 +15,13 @@ class BasicMockedTest(unittest.TestCase):

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)
logger_name = "{}:{}".format(file_name, class_name)
LogConfig.set_logging(loggerName = logger_name)
63 changes: 25 additions & 38 deletions ptf/utest/LogConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,39 @@
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()

def set_logging(loggerName='main'):
"""Set the logger.
"""

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 formatters and add it to handlers
c_debug_format = '%(name)s - %(filename)s - %(levelname)s - %(message)s'
c_error_format = '%(filename)s - %(levelname)s - %(message)s'
f_format = '%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(message)s'
datafmt = '%m/%d/%Y %I:%M:%S %p'

# 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')
# Create a custom logger
logger = logging.getLogger()
logger.name = loggerName
logger.setLevel(logging.INFO)

console_error_handler.setFormatter(c_error_format)
console_debug_handler.setFormatter(c_debug_format)
file_handler.setFormatter(f_format)
file_formatter = logging.Formatter(f_format, datafmt)

# Add handlers to the logger
logger.addHandler(console_debug_handler)
# Create handlers
# Add a new file handler
file_handler = logging.FileHandler(LOG_FILE_PATH)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
logger.addHandler(console_error_handler)


def rollover_log():
file_handler.doRollover()
# Add a new stream handler
console_formatter = logging.Formatter(
c_error_format, datafmt)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.ERROR)
stream_handler.setFormatter(console_formatter)
logger.addHandler(stream_handler)
7 changes: 5 additions & 2 deletions ptf/utest/TemplateTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ def setUp(self):


def test_logger(self):
self.set_logger_name()
sai_thrift_remove_acl_table(self.client, 11111)
self.check_file_contains(LOG_FILE_PATH, 'acl_table_oid')

sai_thrift_create_switch(self.client, init_switch=True, hardware_access_bus="11:11:11:11:11:11")
sai_thrift_create_switch(self.client,
init_switch=True,
hardware_access_bus="11:11:11:11:11:11")
self.check_file_contains(LOG_FILE_PATH, 'hardware_access_bus')


Expand All @@ -41,7 +44,7 @@ def check_file_contains(self, file, content):
datafile = f.readlines()
found = False
for line in datafile:
if content in line:
if content in line:
return True
return False

Expand Down
63 changes: 25 additions & 38 deletions test/sai_test/LogConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,39 @@
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()

def set_logging(loggerName='main'):
"""Set the logger.
"""

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 formatters and add it to handlers
c_debug_format = '%(name)s - %(filename)s - %(levelname)s - %(message)s'
c_error_format = '%(filename)s - %(levelname)s - %(message)s'
f_format = '%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(message)s'
datafmt = '%m/%d/%Y %I:%M:%S %p'

# 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')
# Create a custom logger
logger = logging.getLogger()
logger.name = loggerName
logger.setLevel(logging.INFO)

console_error_handler.setFormatter(c_error_format)
console_debug_handler.setFormatter(c_debug_format)
file_handler.setFormatter(f_format)
file_formatter = logging.Formatter(f_format, datafmt)

# Add handlers to the logger
logger.addHandler(console_debug_handler)
# Create handlers
# Add a new file handler
file_handler = logging.FileHandler(LOG_FILE_PATH)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
logger.addHandler(console_error_handler)


def rollover_log():
file_handler.doRollover()
# Add a new stream handler
console_formatter = logging.Formatter(
c_error_format, datafmt)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.ERROR)
stream_handler.setFormatter(console_formatter)
logger.addHandler(stream_handler)
5 changes: 2 additions & 3 deletions test/sai_test/sai_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@

import inspect

from LogConfig import logger
import LogConfig

THRIFT_PORT = 9092
Expand Down Expand Up @@ -358,10 +357,10 @@ 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)
logger_name = "{}:{}".format(file_name, class_name)
LogConfig.set_logging(loggerName = logger_name)

def setUp(self,
force_config=False,
Expand Down

0 comments on commit 9723951

Please sign in to comment.