-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_app.rb
60 lines (51 loc) · 1.1 KB
/
example_app.rb
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
# frozen_string_literal: true
require_relative 'lib/twiglet/logger'
PORT = 8080
logger = Twiglet::Logger.new('petshop')
# Start our petshop
logger.info(
{
event: {
action: 'startup'
},
message: "Ready to go, listening on port #{PORT}",
server: {
port: PORT
}
}
)
# Use text logging
logger.info("Ready to go, listening on port #{PORT}")
#
# We get a request
request_logger = logger.with(
{
event: {
action: 'HTTP request'
},
trace: {
id: '126bb6fa-28a2-470f-b013-eefbf9182b2d'
}
}
)
# Oh noes!
db_err = StandardError.new('Connection timed-out')
request_logger.error({ message: 'DB connection failed.' }, db_err) if db_err
# We return an error to the requester
request_logger.info(
{
message: 'Internal Server Error',
http: {
request: {
method: 'get'
},
response: {
status_code: 500
}
}
}
)
# Logging with an empty message is an anti-pattern and is therefore forbidden
# Both of the following lines would throw an error
# request_logger.error({ message: "" })
# logger.debug({ message: " " })