Skip to content

Commit df2e19b

Browse files
committed
Logging Code Snippets
1 parent 937a90a commit df2e19b

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed

Diff for: Logging-Advanced/employee.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import logging
2+
3+
logger = logging.getLogger(__name__)
4+
logger.setLevel(logging.INFO)
5+
6+
formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
7+
8+
file_handler = logging.FileHandler('employee.log')
9+
file_handler.setFormatter(formatter)
10+
11+
logger.addHandler(file_handler)
12+
13+
14+
class Employee:
15+
"""A sample Employee class"""
16+
17+
def __init__(self, first, last):
18+
self.first = first
19+
self.last = last
20+
21+
logger.info('Created Employee: {} - {}'.format(self.fullname, self.email))
22+
23+
@property
24+
def email(self):
25+
return '{}.{}@email.com'.format(self.first, self.last)
26+
27+
@property
28+
def fullname(self):
29+
return '{} {}'.format(self.first, self.last)
30+
31+
32+
emp_1 = Employee('John', 'Smith')
33+
emp_2 = Employee('Corey', 'Schafer')
34+
emp_3 = Employee('Jane', 'Doe')

Diff for: Logging-Advanced/log-sample.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import logging
2+
import employee
3+
4+
logger = logging.getLogger(__name__)
5+
logger.setLevel(logging.DEBUG)
6+
7+
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
8+
9+
file_handler = logging.FileHandler('sample.log')
10+
file_handler.setLevel(logging.ERROR)
11+
file_handler.setFormatter(formatter)
12+
13+
stream_handler = logging.StreamHandler()
14+
stream_handler.setFormatter(formatter)
15+
16+
logger.addHandler(file_handler)
17+
logger.addHandler(stream_handler)
18+
19+
20+
def add(x, y):
21+
"""Add Function"""
22+
return x + y
23+
24+
25+
def subtract(x, y):
26+
"""Subtract Function"""
27+
return x - y
28+
29+
30+
def multiply(x, y):
31+
"""Multiply Function"""
32+
return x * y
33+
34+
35+
def divide(x, y):
36+
"""Divide Function"""
37+
try:
38+
result = x / y
39+
except ZeroDivisionError:
40+
logger.exception('Tried to divide by zero')
41+
else:
42+
return result
43+
44+
45+
num_1 = 10
46+
num_2 = 0
47+
48+
add_result = add(num_1, num_2)
49+
logger.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
50+
51+
sub_result = subtract(num_1, num_2)
52+
logger.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
53+
54+
mul_result = multiply(num_1, num_2)
55+
logger.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
56+
57+
div_result = divide(num_1, num_2)
58+
logger.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))

Diff for: Logging-Basics/employee.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
import logging
3+
4+
logging.basicConfig(filename='employee.log', level=logging.INFO,
5+
format='%(levelname)s:%(message)s')
6+
7+
8+
class Employee:
9+
"""A sample Employee class"""
10+
11+
def __init__(self, first, last):
12+
self.first = first
13+
self.last = last
14+
15+
logging.info('Created Employee: {} - {}'.format(self.fullname, self.email))
16+
17+
@property
18+
def email(self):
19+
return '{}.{}@email.com'.format(self.first, self.last)
20+
21+
@property
22+
def fullname(self):
23+
return '{} {}'.format(self.first, self.last)
24+
25+
26+
emp_1 = Employee('John', 'Smith')
27+
emp_2 = Employee('Corey', 'Schafer')
28+
emp_3 = Employee('Jane', 'Doe')

Diff for: Logging-Basics/log-sample.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
import logging
3+
4+
# DEBUG: Detailed information, typically of interest only when diagnosing problems.
5+
6+
# INFO: Confirmation that things are working as expected.
7+
8+
# 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.
9+
10+
# ERROR: Due to a more serious problem, the software has not been able to perform some function.
11+
12+
# CRITICAL: A serious error, indicating that the program itself may be unable to continue running.
13+
14+
logging.basicConfig(filename='test.log', level=logging.DEBUG,
15+
format='%(asctime)s:%(levelname)s:%(message)s')
16+
17+
18+
def add(x, y):
19+
"""Add Function"""
20+
return x + y
21+
22+
23+
def subtract(x, y):
24+
"""Subtract Function"""
25+
return x - y
26+
27+
28+
def multiply(x, y):
29+
"""Multiply Function"""
30+
return x * y
31+
32+
33+
def divide(x, y):
34+
"""Divide Function"""
35+
return x / y
36+
37+
38+
num_1 = 20
39+
num_2 = 10
40+
41+
add_result = add(num_1, num_2)
42+
logging.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
43+
44+
sub_result = subtract(num_1, num_2)
45+
logging.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
46+
47+
mul_result = multiply(num_1, num_2)
48+
logging.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
49+
50+
div_result = divide(num_1, num_2)
51+
logging.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))

Diff for: Logging-Basics/snippets.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
# DEBUG: Detailed information, typically of interest only when diagnosing problems.
3+
4+
# INFO: Confirmation that things are working as expected.
5+
6+
# 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.
7+
8+
# ERROR: Due to a more serious problem, the software has not been able to perform some function.
9+
10+
# CRITICAL: A serious error, indicating that the program itself may be unable to continue running.

0 commit comments

Comments
 (0)