Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdf2e19b

Browse files
committed
Logging Code Snippets
1 parent937a90a commitdf2e19b

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed

‎Logging-Advanced/employee.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
importlogging
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+
classEmployee:
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+
defemail(self):
25+
return'{}.{}@email.com'.format(self.first,self.last)
26+
27+
@property
28+
deffullname(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')

‎Logging-Advanced/log-sample.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
importlogging
2+
importemployee
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+
defadd(x,y):
21+
"""Add Function"""
22+
returnx+y
23+
24+
25+
defsubtract(x,y):
26+
"""Subtract Function"""
27+
returnx-y
28+
29+
30+
defmultiply(x,y):
31+
"""Multiply Function"""
32+
returnx*y
33+
34+
35+
defdivide(x,y):
36+
"""Divide Function"""
37+
try:
38+
result=x/y
39+
exceptZeroDivisionError:
40+
logger.exception('Tried to divide by zero')
41+
else:
42+
returnresult
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))

‎Logging-Basics/employee.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
importlogging
3+
4+
logging.basicConfig(filename='employee.log',level=logging.INFO,
5+
format='%(levelname)s:%(message)s')
6+
7+
8+
classEmployee:
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+
defemail(self):
19+
return'{}.{}@email.com'.format(self.first,self.last)
20+
21+
@property
22+
deffullname(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')

‎Logging-Basics/log-sample.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
importlogging
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+
defadd(x,y):
19+
"""Add Function"""
20+
returnx+y
21+
22+
23+
defsubtract(x,y):
24+
"""Subtract Function"""
25+
returnx-y
26+
27+
28+
defmultiply(x,y):
29+
"""Multiply Function"""
30+
returnx*y
31+
32+
33+
defdivide(x,y):
34+
"""Divide Function"""
35+
returnx/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))

‎Logging-Basics/snippets.txt

Lines changed: 10 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp