-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlogging_test.rb
More file actions
96 lines (83 loc) · 3.15 KB
/
logging_test.rb
File metadata and controls
96 lines (83 loc) · 3.15 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# typed: true
# frozen_string_literal: true
require "test_helper"
module LogStruct
module CodeExamples
class LoggingTest < ActiveSupport::TestCase
class MockUser < T::Struct
const :id, Integer
const :email, String
end
class MockRequest < T::Struct
const :remote_ip, String
end
def test_basic_logging
user = MockUser.new(id: 123, email: "user@example.com")
# ----------------------------------------------------------
# BEGIN CODE EXAMPLE: basic_logging
# ----------------------------------------------------------
# Log a simple string
Rails.logger.info "User signed in"
# Log a hash with custom fields
Rails.logger.info({
event: "user_login",
user_id: user.id,
ip_address: "192.168.1.1",
custom_field: "any value you want"
})
# ----------------------------------------------------------
# END CODE EXAMPLE: basic_logging
# ----------------------------------------------------------
assert_equal 123, user.id # Stop minitest complaining
end
def test_getting_started_basic_logging
user = MockUser.new(id: 123, email: "user@example.com")
request = MockRequest.new(remote_ip: "192.168.1.1")
# Set up tagged logger
Rails.logger = ActiveSupport::TaggedLogging.new(Rails.logger)
# ----------------------------------------------------------
# BEGIN CODE EXAMPLE: getting_started_basic_logging
# ----------------------------------------------------------
# Log a simple message
Rails.logger.info "User signed in"
# Log structured data
Rails.logger.info({
src: "rails",
evt: "user_login",
user_id: user.id,
ip_address: request.remote_ip
})
# Log with tags
Rails.logger.tagged("Authentication") do
Rails.logger.info "User signed in"
Rails.logger.info(user_id: user.id, ip_address: request.remote_ip)
end
# ----------------------------------------------------------
# END CODE EXAMPLE: getting_started_basic_logging
# ----------------------------------------------------------
assert_equal 123, user.id # Stop minitest complaining
end
def test_typed_logging
# ----------------------------------------------------------
# BEGIN CODE EXAMPLE: typed_logging
# ----------------------------------------------------------
# Create a typed log entry
log = LogStruct::Log::Plain.new(
source: LogStruct::Source::App,
message: "User signed in",
additional_data: {
user_id: 123,
ip_address: "192.168.1.1"
}
)
# Log the typed struct
LogStruct.info(log)
# ----------------------------------------------------------
# END CODE EXAMPLE: typed_logging
# ----------------------------------------------------------
# Simple assertion to make the test pass
assert_instance_of LogStruct::Log::Plain, log
end
end
end
end