Python - Logging Advanced : Loggers, Handlers, and Formatters
** Logging Advanced - Loggers, Handlers, and Formatters
==========================================
import logging
logging.basicConfig(filename='employee.log', level=logging.INFO,
format='%(levelname)s:%(name)s:%(message)s')
class Employee:
"""A sample Employee class"""
def __init__(self, first, last):
self.first = first
self.last = last
logging.info('Created Employee: {} - {}'.format(self.fullname, self.email))
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('John', 'Smith')
emp_2 = Employee('Corey', 'Schafer')
emp_3 = Employee('Jane', 'Doe')
---------employee.log---------------
INFO:root:Created Employee: John Smith - John.Smith@email.com
INFO:root:Created Employee: Corey Schafer - Corey.Schafer@email.com
INFO:root:Created Employee: Jane Doe - Jane.Doe@email.com
---------------------------------
==========================================
import logging
logging.basicConfig(filename='sample.log', level = logging.DEBUG,
format = '%(asctime)s:%(name)s:%(message)s')
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
return x / y
num_1 = 20
num_2 = 10
add_result = add(num_1, num_2)
logging.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logging.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logging.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logging.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))
----------sample.log---------------
2020-05-19 18:22:39,155:root:Add: 20 + 10 = 30
2020-05-19 18:22:39,155:root:Sub: 20 - 10 = 10
2020-05-19 18:22:39,155:root:Mul: 20 * 10 = 200
2020-05-19 18:22:39,156:root:Div: 20 / 10 = 2.0
---------------------------------
==========================================
import logging
import employee
logging.basicConfig(filename='sample.log', level = logging.DEBUG,
format = '%(asctime)s:%(name)s:%(message)s')
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
return x / y
num_1 = 20
num_2 = 10
add_result = add(num_1, num_2)
logging.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logging.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logging.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logging.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))
----------employee.py---------------
import logging
logging.basicConfig(filename='employee.log', level=logging.INFO,
format='%(levelname)s:%(name)s:%(message)s')
class Employee:
"""A sample Employee class"""
def __init__(self, first, last):
self.first = first
self.last = last
logging.info('Created Employee: {} - {}'.format(self.fullname, self.email))
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('John', 'Smith')
emp_2 = Employee('Corey', 'Schafer')
emp_3 = Employee('Jane', 'Doe')
---------employee.log---------------
INFO:root:Created Employee: John Smith - John.Smith@email.com
INFO:root:Created Employee: Corey Schafer - Corey.Schafer@email.com
INFO:root:Created Employee: Jane Doe - Jane.Doe@email.com
---------------------------------
==========================================
import logging
import employee
logging.basicConfig(filename='sample.log', level = logging.DEBUG,
format = '%(asctime)s:%(name)s:%(message)s')
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
return x / y
num_1 = 20
num_2 = 10
add_result = add(num_1, num_2)
logging.info('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logging.info('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logging.info('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logging.info('Div: {} / {} = {}'.format(num_1, num_2, div_result))
----------employee.py---------------
import logging
logging.basicConfig(filename='employee.log', level=logging.INFO,
format='%(levelname)s:%(name)s:%(message)s')
class Employee:
"""A sample Employee class"""
def __init__(self, first, last):
self.first = first
self.last = last
logging.info('Created Employee: {} - {}'.format(self.fullname, self.email))
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('John', 'Smith')
emp_2 = Employee('Corey', 'Schafer')
emp_3 = Employee('Jane', 'Doe')
---------employee.log---------------
INFO:root:Created Employee: John Smith - John.Smith@email.com
INFO:root:Created Employee: Corey Schafer - Corey.Schafer@email.com
INFO:root:Created Employee: Jane Doe - Jane.Doe@email.com
INFO:root:Add: 20 + 10 = 30
INFO:root:Sub: 20 - 10 = 10
INFO:root:Mul: 20 * 10 = 200
INFO:root:Div: 20 / 10 = 2.0
---------------------------------
==========================================
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(filename='employee.log', level=logging.INFO,
format='%(levelname)s:%(name)s:%(message)s')
class Employee:
"""A sample Employee class"""
def __init__(self, first, last):
self.first = first
self.last = last
logger.info('Created Employee: {} - {}'.format(self.fullname, self.email))
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('John', 'Smith')
emp_2 = Employee('Corey', 'Schafer')
emp_3 = Employee('Jane', 'Doe')
---------employee.log---------------
INFO:__main__:Created Employee: John Smith - John.Smith@email.com
INFO:__main__:Created Employee: Corey Schafer - Corey.Schafer@email.com
INFO:__main__:Created Employee: Jane Doe - Jane.Doe@email.com
---------------------------------
==========================================
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('employee.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
class Employee:
"""A sample Employee class"""
def __init__(self, first, last):
self.first = first
self.last = last
logger.info('Created Employee: {} - {}'.format(self.fullname, self.email))
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('John', 'Smith')
emp_2 = Employee('Corey', 'Schafer')
emp_3 = Employee('Jane', 'Doe')
---------employee.log---------------
INFO:__main__:Created Employee: John Smith - John.Smith@email.com
INFO:__main__:Created Employee: Corey Schafer - Corey.Schafer@email.com
INFO:__main__:Created Employee: Jane Doe - Jane.Doe@email.com
---------------------------------
==========================================
import logging
import employee
logging.basicConfig(filename='sample.log', level = logging.DEBUG,
format = '%(asctime)s:%(name)s:%(message)s')
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
return x / y
num_1 = 20
num_2 = 10
add_result = add(num_1, num_2)
logging.info('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logging.info('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logging.info('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logging.info('Div: {} / {} = {}'.format(num_1, num_2, div_result))
---------employee.py----------------
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('employee.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
class Employee:
"""A sample Employee class"""
def __init__(self, first, last):
self.first = first
self.last = last
logger.info('Created Employee: {} - {}'.format(self.fullname, self.email))
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('John', 'Smith')
emp_2 = Employee('Corey', 'Schafer')
emp_3 = Employee('Jane', 'Doe')
-----------sample.log---------------
2020-05-19 18:40:59,594:root:Add: 20 + 10 = 30
2020-05-19 18:40:59,594:root:Sub: 20 - 10 = 10
2020-05-19 18:40:59,594:root:Mul: 20 * 10 = 200
2020-05-19 18:40:59,594:root:Div: 20 / 10 = 2.0
---------employee.log---------------
INFO:employee:Created Employee: John Smith - John.Smith@email.com
INFO:employee:Created Employee: Corey Schafer - Corey.Schafer@email.com
INFO:employee:Created Employee: Jane Doe - Jane.Doe@email.com
---------------------------------
==========================================
import logging
import employee
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('sample.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
return x / y
num_1 = 20
num_2 = 10
add_result = add(num_1, num_2)
logger.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logger.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logger.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logger.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))
---------employee.py----------------
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('employee.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
class Employee:
"""A sample Employee class"""
def __init__(self, first, last):
self.first = first
self.last = last
logger.info('Created Employee: {} - {}'.format(self.fullname, self.email))
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('John', 'Smith')
emp_2 = Employee('Corey', 'Schafer')
emp_3 = Employee('Jane', 'Doe')
-----------sample.log---------------
2020-05-19 18:50:07,802:__main__:Add: 20 + 10 = 30
2020-05-19 18:50:07,802:__main__:Sub: 20 - 10 = 10
2020-05-19 18:50:07,803:__main__:Mul: 20 * 10 = 200
2020-05-19 18:50:07,803:__main__:Div: 20 / 10 = 2.0
---------employee.log---------------
INFO:employee:Created Employee: John Smith - John.Smith@email.com
INFO:employee:Created Employee: Corey Schafer - Corey.Schafer@email.com
INFO:employee:Created Employee: Jane Doe - Jane.Doe@email.com
---------------------------------
==========================================
import logging
import employee
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('sample.log')
file_handler.setLevel(logging.ERROR)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
return x / y
num_1 = 20
num_2 = 10
add_result = add(num_1, num_2)
logger.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logger.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logger.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logger.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))
-----------sample.log---------------
<~~ ERROR 가 없기 때문에, sample.log 파일에 내용 없음
---------------------------------
==========================================
import logging
import employee
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('sample.log')
file_handler.setLevel(logging.ERROR)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
try:
result = x / y
except ZeroDivisionError:
logger.error('Tried to divide by zero')
else:
return resule
num_1 = 20
num_2 = 0
add_result = add(num_1, num_2)
logger.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logger.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logger.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logger.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))
-----------sample.log---------------
2020-05-19 18:59:07,508:__main__:Tried to divide by zero
---------------------------------
==========================================
import logging
import employee
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('sample.log')
file_handler.setLevel(logging.ERROR)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
try:
result = x / y
except ZeroDivisionError:
logger.exception('Tried to divide by zero')
else:
return resule
num_1 = 20
num_2 = 0
add_result = add(num_1, num_2)
logger.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logger.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logger.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logger.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))
-----------sample.log---------------
2020-05-19 19:15:59,921:__main__:Tried to divide by zero
Traceback (most recent call last):
File "C:\Users\purunet\Documents\py6\log1.py", line 37, in divide
result = x / y
ZeroDivisionError: division by zero
---------------------------------
==========================================
import logging
import employee
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('sample.log')
file_handler.setLevel(logging.ERROR)
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
try:
result = x / y
except ZeroDivisionError:
logger.exception('Tried to divide by zero')
else:
return resule
num_1 = 20
num_2 = 0
add_result = add(num_1, num_2)
logger.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logger.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logger.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logger.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))
---------------------------------
Add: 20 + 0 = 20
Sub: 20 - 0 = 20
Mul: 20 * 0 = 0
Tried to divide by zero
Traceback (most recent call last):
File "C:\Users\purunet\Documents\py6\log1.py", line 39, in divide
result = x / y
ZeroDivisionError: division by zero
Div: 20 / 0 = None
---------------------------------
==========================================
import logging
import employee
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('sample.log')
file_handler.setLevel(logging.ERROR)
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
try:
result = x / y
except ZeroDivisionError:
logger.exception('Tried to divide by zero')
else:
return resule
num_1 = 20
num_2 = 0
add_result = add(num_1, num_2)
logger.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logger.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logger.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logger.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))
---------------------------------
2020-05-19 19:20:20,756:__main__:Add: 20 + 0 = 20
2020-05-19 19:20:20,756:__main__:Sub: 20 - 0 = 20
2020-05-19 19:20:20,757:__main__:Mul: 20 * 0 = 0
2020-05-19 19:20:20,758:__main__:Tried to divide by zero
Traceback (most recent call last):
File "C:\Users\purunet\Documents\py6\log1.py", line 40, in divide
result = x / y
ZeroDivisionError: division by zero
2020-05-19 19:20:20,762:__main__:Div: 20 / 0 = None
---------------------------------