Skip to content

Commit 89b0d26

Browse files
committed
add logging tutorial
1 parent c5d8b61 commit 89b0d26

File tree

8 files changed

+118
-0
lines changed

8 files changed

+118
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
109109
- [How to Download and Upload Files in FTP Server using Python](https://www.thepythoncode.com/article/download-and-upload-files-in-ftp-server-using-python). ([code](python-standard-library/download-and-upload-files-in-ftp))
110110
- [How to Work with JSON Files in Python](https://www.thepythoncode.com/article/working-with-json-files-in-python). ([code](python-standard-library/working-with-json))
111111
- [How to Use Regular Expressions in Python](https://www.thepythoncode.com/article/work-with-regular-expressions-in-python). ([code](python-standard-library/regular-expressions))
112+
- [Logging in Python](https://www.thepythoncode.com/article/logging-in-python). ([code](python-standard-library/logging))
112113

113114
- ### [Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
114115
- [How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [Logging in Python](https://www.thepythoncode.com/article/logging-in-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import logging
2+
3+
# make a basic logging configuration
4+
# here we set the level of logging to DEBUG
5+
logging.basicConfig(
6+
level=logging.DEBUG
7+
)
8+
9+
# make a debug message
10+
logging.debug("This is a simple debug log")
11+
12+
# make an info message
13+
logging.info("This is a simple info log")
14+
15+
# make a warning message
16+
logging.warning("This is a simple warning log")
17+
18+
# make an error message
19+
logging.error("This is a simple error log")
20+
21+
# make a critical message
22+
logging.critical("This is a simple critical log")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import logging
2+
3+
logging.basicConfig(
4+
level=logging.INFO,
5+
format="%(asctime)s - %(levelname)s - %(message)s",
6+
)
7+
8+
logging.info("This is an info message!")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import logging
2+
3+
logging.basicConfig(
4+
level=logging.INFO,
5+
format="%(asctime)s - %(levelname)s - %(message)s",
6+
datefmt="[%Y-%m-%d] %H:%M:%S",
7+
)
8+
9+
logging.info("This is an info message!")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import logging
2+
import math
3+
4+
logging.basicConfig(level=logging.DEBUG,
5+
handlers=[logging.FileHandler('logs.log', 'a', 'utf-8')],
6+
format="%(asctime)s %(levelname)-6s - %(funcName)-8s - %(filename)s - %(lineno)-3d - %(message)s",
7+
datefmt="[%Y-%m-%d] %H:%M:%S - ",
8+
)
9+
10+
logging.info("This is an info log")
11+
12+
def square_root(x):
13+
logging.debug(f"Getting the square root of {x}")
14+
try:
15+
result = math.sqrt(x)
16+
except ValueError:
17+
logging.exception("Cannot get square root of a negative number")
18+
# or
19+
# logging.error("Cannot get square root of a negative number", exc_info=True)
20+
return None
21+
logging.info(f"The square root of {x} is {result:.5f}")
22+
return result
23+
24+
square_root(5)
25+
square_root(-5)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import logging
2+
3+
# return a logger with the specified name & creating it if necessary
4+
logger = logging.getLogger(__name__)
5+
6+
# create a logger handler, in this case: file handler
7+
file_handler = logging.FileHandler("file.log")
8+
# set the level of logging to INFO
9+
file_handler.setLevel(logging.INFO)
10+
11+
# create a logger formatter
12+
logging_format = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
13+
14+
# add the format to the logger handler
15+
file_handler.setFormatter(logging_format)
16+
17+
# add the handler to the logger
18+
logger.addHandler(file_handler)
19+
20+
# use the logger as previously
21+
logger.critical("This is a critical message!")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import logging
2+
3+
# make a debug message
4+
logging.debug("This is a simple debug log")
5+
6+
# make an info message
7+
logging.info("This is a simple info log")
8+
9+
# make a warning message
10+
logging.warning("This is a simple warning log")
11+
12+
# make an error message
13+
logging.error("This is a simple error log")
14+
15+
# make a critical message
16+
logging.critical("This is a simple critical log")
17+
18+
# just mapping logging level integers into strings for convenience
19+
logging_levels = {
20+
logging.DEBUG: "DEBUG", # 10
21+
logging.INFO: "INFO", # 20
22+
logging.WARNING: "WARNING", # 30
23+
logging.ERROR: "ERROR", # 40
24+
logging.CRITICAL: "CRITICAL", # 50
25+
}
26+
27+
# get the current logging level
28+
print("Current logging level:", logging_levels.get(logging.root.level))
29+
30+
# get the current logging format
31+
print("Current logging format:", logging.BASIC_FORMAT)

0 commit comments

Comments
 (0)