레이블이 logging인 게시물을 표시합니다. 모든 게시물 표시
레이블이 logging인 게시물을 표시합니다. 모든 게시물 표시

5/19/2020

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

---------------------------------

Python - Logging Basics : Logging to Files, Setting Levels, and Formatting

** Logging Basics - Logging to Files, Setting Levels, and Formatting



==========================================

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 = 10
num_2 = 5


add_result = add(num_1, num_2)
print('Add: {} + {} = {}'.format(num_1, num_2, add_result))

sub_result = subtract(num_1, num_2)
print('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))

mul_result = multiply(num_1, num_2)
print('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))

div_result = divide(num_1, num_2)
print('Div: {} / {} = {}'.format(num_1, num_2, div_result))

---------------------------------

Add: 10 + 5 = 15
Sub: 10 - 5 = 5
Mul: 10 * 5 = 50
Div: 10 / 5 = 2.0

---------------------------------

==========================================

import logging

# DEBUG: Detailed information, typically of interest only when diagnosing problems.

# INFO: Confirmation that things are working as expected.

# WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

# ERROR: Due to a more serious problem, the software has not been able to perform some function.

# CRITICAL: A serious error, indicating that the program itself may be unable to continue running.


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 = 10
num_2 = 5


add_result = add(num_1, num_2)
logging.warning('Add: {} + {} = {}'.format(num_1, num_2, add_result))

sub_result = subtract(num_1, num_2)
logging.warning('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))

mul_result = multiply(num_1, num_2)
logging.warning('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))

div_result = divide(num_1, num_2)
logging.warning('Div: {} / {} = {}'.format(num_1, num_2, div_result))

---------------------------------

WARNING:root:Add: 10 + 5 = 15
WARNING:root:Sub: 10 - 5 = 5
WARNING:root:Mul: 10 * 5 = 50
WARNING:root:Div: 10 / 5 = 2.0

---------------------------------

==========================================

import logging

# DEBUG: Detailed information, typically of interest only when diagnosing problems.

# INFO: Confirmation that things are working as expected.

# WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

# ERROR: Due to a more serious problem, the software has not been able to perform some function.

# CRITICAL: A serious error, indicating that the program itself may be unable to continue running.


logging.basicConfig(level = logging.DEBUG)

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 = 10
num_2 = 5


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))

---------------------------------

DEBUG:root:Add: 10 + 5 = 15
DEBUG:root:Sub: 10 - 5 = 5
DEBUG:root:Mul: 10 * 5 = 50
DEBUG:root:Div: 10 / 5 = 2.0

---------------------------------

==========================================

import logging

# DEBUG: Detailed information, typically of interest only when diagnosing problems.

# INFO: Confirmation that things are working as expected.

# WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

# ERROR: Due to a more serious problem, the software has not been able to perform some function.

# CRITICAL: A serious error, indicating that the program itself may be unable to continue running.


logging.basicConfig(filename='test.log', level = logging.DEBUG)

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 = 10
num_2 = 5


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))


---------test.log------------------

DEBUG:root:Add: 10 + 5 = 15
DEBUG:root:Sub: 10 - 5 = 5
DEBUG:root:Mul: 10 * 5 = 50
DEBUG:root:Div: 10 / 5 = 2.0

---------------------------------

==========================================

import logging

# DEBUG: Detailed information, typically of interest only when diagnosing problems.

# INFO: Confirmation that things are working as expected.

# WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

# ERROR: Due to a more serious problem, the software has not been able to perform some function.

# CRITICAL: A serious error, indicating that the program itself may be unable to continue running.


logging.basicConfig(filename='test.log', level = logging.DEBUG)

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))

---------test.log------------------

DEBUG:root:Add: 10 + 5 = 15
DEBUG:root:Sub: 10 - 5 = 5
DEBUG:root:Mul: 10 * 5 = 50
DEBUG:root:Div: 10 / 5 = 2.0
DEBUG:root:Add: 20 + 10 = 30
DEBUG:root:Sub: 20 - 10 = 10
DEBUG:root:Mul: 20 * 10 = 200
DEBUG:root:Div: 20 / 10 = 2.0

---------------------------------

==========================================

https://docs.python.org/3/library/logging.html

==========================================


import logging

# DEBUG: Detailed information, typically of interest only when diagnosing problems.

# INFO: Confirmation that things are working as expected.

# WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

# ERROR: Due to a more serious problem, the software has not been able to perform some function.

# CRITICAL: A serious error, indicating that the program itself may be unable to continue running.


logging.basicConfig(filename='test.log', level = logging.DEBUG,
format = '%(asctime)s:%(levelname)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))


---------test.log------------------

DEBUG:root:Add: 10 + 5 = 15
DEBUG:root:Sub: 10 - 5 = 5
DEBUG:root:Mul: 10 * 5 = 50
DEBUG:root:Div: 10 / 5 = 2.0
DEBUG:root:Add: 20 + 10 = 30
DEBUG:root:Sub: 20 - 10 = 10
DEBUG:root:Mul: 20 * 10 = 200
DEBUG:root:Div: 20 / 10 = 2.0
2020-05-19 18:03:54,008:DEBUG:Add: 20 + 10 = 30
2020-05-19 18:03:54,033:DEBUG:Sub: 20 - 10 = 10
2020-05-19 18:03:54,033:DEBUG:Mul: 20 * 10 = 200
2020-05-19 18:03:54,033:DEBUG:Div: 20 / 10 = 2.0

---------------------------------

==========================================

class Employee:
    """A sample Employee class"""

    def __init__(self, first, last):
        self.first = first
        self.last = last

        print('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')

---------------------------------

Created Employee: John Smith - John.Smith@email.com
Created Employee: Corey Schafer - Corey.Schafer@email.com

---------------------------------

==========================================

import logging

logging.basicConfig(filename = 'employee.log', level = logging.INFO,
format = '%(levelname)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:Created Employee: John Smith - John.Smith@email.com
INFO:Created Employee: Corey Schafer - Corey.Schafer@email.com
INFO:Created Employee: Jane Doe - Jane.Doe@email.com

---------------------------------