8000 Merge pull request #553 from realpython/python-logging-update · realpython/materials@cfa3804 · GitHub
[go: up one dir, main page]

Skip to content

Commit cfa3804

Browse files
authored
Merge pull request #553 from realpython/python-logging-update
Add materials for the logging tutorial
2 parents 2758339 + 2a211e8 commit cfa3804

8 files changed

+97
-0
lines changed

python-logging/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Logging in Python
2+
3+
This folder provides the code examples for the Real Python tutorial [Logging in Python](https://realpython.com/python-logging/).
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import logging
2+
3+
logger = logging.getLogger(__name__)
4+
logger.warning("Look at my logger!")

python-logging/logging_debug.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import logging
2+
3+
logging.basicConfig(level=logging.DEBUG)
4+
logging.debug("This will get logged.")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import logging
2+
3+
logging.basicConfig(
4+
filename="app.log",
5+
encoding="utf-8",
6+
filemode="a",
7+
format="{asctime} - {levelname} - {message}",
8+
style="{",
9+
datefmt="%Y-%m-%d %H:%M",
10+
)
11+
12+
donuts = 5
13+
guests = 0
14+
try:
15+
donuts_per_guest = donuts / guests
16+
except ZeroDivisionError:
17+
logging.error("DonutCalculationError", exc_info=True)

python-logging/logging_style.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import logging
2+
3+
logging.basicConfig(
4+
format="{asctime} - {levelname} - {message}",
5+
style="{",
6+
datefmt="%Y-%m-%d %H:%M",
7+
)
8+
9+
logging.error("Something went wrong!")

python-logging/logging_to_a_file.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import logging
2+
3+
logging.basicConfig(
4+
filename="app.log",
5+
encoding="utf-8",
6+
filemode="a",
7+
format="{asctime} - {levelname} - {message}",
8+
style="{",
9+
datefmt="%Y-%m-%d %H:%M",
10+
)
11+
12+
logging.warning("Save me!")

python-logging/logging_with_filter.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import logging
2+
3+
4+
def show_only_debug(record):
5+
return record.levelname == "DEBUG"
6+
7+
8+
logger = logging.getLogger(__name__)
9+
logger.setLevel("DEBUG")
10+
formatter = logging.Formatter("{levelname} - {message}", style="{")
11+
12+
console_handler = logging.StreamHandler()
13+
console_handler.setLevel("DEBUG")
14+
console_handler.setFormatter(formatter)
15+
console_handler.addFilter(show_only_debug)
16+
logger.addHandler(console_handler)
17+
18+
file_handler = logging.FileHandler("app.log", mode="a", encoding="utf-8")
19+
file_handler.setLevel("WARNING")
20+
file_handler.setFormatter(formatter)
21+
logger.addHandler(file_handler)
22+
23+
logger.debug("Just checking in!")
24+
logger.warning("Stay curious!")
25+
logger.error("Stay put!")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import logging
2+
3+
logger = logging.getLogger(__name__)
4+
logger.setLevel("DEBUG")
5+
formatter = logging.Formatter("{levelname} - {message}", style="{")
6+
7+
console_handler = logging.StreamHandler()
8+
console_handler.setLevel("DEBUG")
9+
console_handler.setFormatter(formatter)
10+
logger.addHandler(console_handler)
11+
12+
file_handler = logging.FileHandler("app.log", mode="a", encoding="utf-8")
13+
file_handler.setLevel("WARNING")
14+
file_handler.setFormatter(formatter)
15+
logger.addHandler(file_handler)
16+
17+
logger.debug("Just checking in!")
18+
19+
20+
logger.warning("Stay curious!")
21+
22+
23+
logger.error("Stay put!")

0 commit comments

Comments
 (0)
0