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