Skip to content

Commit 5a40766

Browse files
authored
Create example-project.py
1 parent 79fdf20 commit 5a40766

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

example-project/example-project.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# This is an example project of Logtail python integration
2+
# This project shocases how to use Logtail in your python projects
3+
# For more infromation please visit https://github.com/logtail/logtail-python
4+
5+
# SETUP
6+
7+
# Import Logtail client library and deault logging library
8+
from logtail import LogtailHandler
9+
import logging
10+
import sys
11+
12+
# Check for program arguments
13+
if len(sys.argv) != 2:
14+
print("Program requires source token as an argument, run the program as followed\npython example-project.py <source-token>");
15+
sys.exit();
16+
17+
# Create handler
18+
handler = LogtailHandler(source_token=sys.argv[1])
19+
20+
# Create logger
21+
logger = logging.getLogger(__name__)
22+
logger.handlers = []
23+
logger.setLevel(logging.DEBUG) # Set minimal log level
24+
logger.addHandler(handler) # asign handler to logger
25+
26+
# LOGGING EXAMPLE
27+
# Following code shocases logger usage
28+
29+
# Send debug log using the debug() method
30+
logger.debug('I am using Logtail!')
31+
32+
# Send info level log about interesting events using the info() method
33+
logger.info('I love Logtail!')
34+
35+
# Send warning level log about warrying events using the warning() method
36+
# You can also add ecustom structured information to the log by passing it as a second argument
37+
logger.warning('Log structured data', extra={
38+
'item': {
39+
'url': "https://fictional-store.com/item-123",
40+
'price': 100.00
41+
}
42+
})
43+
44+
# Send error level log about errors in runtime using the error() method
45+
logger.error('Oops! An error occured!')
46+
47+
# Send critical level log about critical events in runtume using the critical() method
48+
logger.critical('Its not working, needs to be fixes ASP!')
49+
50+
# Send exception level log about errors in runtime using the exception() method
51+
# Error level log will be sent. Exception info is added to the logging message.
52+
# This method should only be called from an exception handler.
53+
try:
54+
nonexisting_function() # Calling nonexisting function
55+
except Exception as Argument:
56+
logger.exception("Error occurred while calling non-existing function") # Additional info will be added
57+
# OUTPUT:
58+
# Error occurred while calling non-existing function
59+
#Traceback (most recent call last):
60+
# File "logtail.py", line 48, in
61+
# nonexisting_function()
62+
#NameError: name 'nonexisting_function' is not defined
63+
64+
print('All done! You can check your logs now.')

0 commit comments

Comments
 (0)