|
1 |
| -struct SentryLog: Codable { |
2 |
| - let timestamp: Date |
3 |
| - let traceId: SentryId |
4 |
| - let level: SentryLog.Level |
5 |
| - let body: String |
6 |
| - let attributes: [String: SentryLog.Attribute] |
7 |
| - let severityNumber: Int? |
| 1 | +/// A structured log entry that captures log data with associated attribute metadata. |
| 2 | +/// |
| 3 | +/// Use the `options.beforeSendLog` callback to modify or filter log data. |
| 4 | +@objc |
| 5 | +@objcMembers |
| 6 | +public final class SentryLog: NSObject { |
| 7 | + /// The timestamp when the log event occurred |
| 8 | + public var timestamp: Date |
| 9 | + /// The trace ID to associate this log with distributed tracing |
| 10 | + public var traceId: SentryId |
| 11 | + /// The severity level of the log entry |
| 12 | + public var level: Level |
| 13 | + /// The main log message content |
| 14 | + public var body: String |
| 15 | + /// A dictionary of structured attributes added to the log entry |
| 16 | + public var attributes: [String: Attribute] |
| 17 | + /// Numeric representation of the severity level (Int) |
| 18 | + public var severityNumber: NSNumber? |
8 | 19 |
|
9 |
| - private enum CodingKeys: String, CodingKey { |
10 |
| - case timestamp |
11 |
| - case traceId = "trace_id" |
12 |
| - case level |
13 |
| - case body |
14 |
| - case attributes |
15 |
| - case severityNumber = "severity_number" |
16 |
| - } |
17 |
| - |
18 |
| - /// The traceId is initially an empty default value and is populated during processing; |
19 |
| - /// by the time processing completes, it is guaranteed to be a valid non-empty trace id. |
20 |
| - init( |
| 20 | + internal init( |
21 | 21 | timestamp: Date,
|
22 | 22 | traceId: SentryId,
|
23 |
| - level: SentryLog.Level, |
| 23 | + level: Level, |
24 | 24 | body: String,
|
25 |
| - attributes: [String: SentryLog.Attribute], |
26 |
| - severityNumber: Int? = nil |
| 25 | + attributes: [String: Attribute], |
| 26 | + severityNumber: NSNumber? = nil |
27 | 27 | ) {
|
28 | 28 | self.timestamp = timestamp
|
29 | 29 | self.traceId = traceId
|
30 | 30 | self.level = level
|
31 | 31 | self.body = body
|
32 | 32 | self.attributes = attributes
|
33 |
| - self.severityNumber = severityNumber ?? level.toSeverityNumber() |
| 33 | + self.severityNumber = severityNumber ?? NSNumber(value: level.toSeverityNumber()) |
| 34 | + super.init() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// MARK: - Internal Codable Support |
| 39 | +@_spi(Private) extension SentryLog: Codable { |
| 40 | + private enum CodingKeys: String, CodingKey { |
| 41 | + case timestamp |
| 42 | + case traceId = "trace_id" |
| 43 | + case level |
| 44 | + case body |
| 45 | + case attributes |
| 46 | + case severityNumber = "severity_number" |
34 | 47 | }
|
35 | 48 |
|
36 |
| - init(from decoder: any Decoder) throws { |
| 49 | + @_spi(Private) public convenience init(from decoder: any Decoder) throws { |
37 | 50 | let container = try decoder.container(keyedBy: CodingKeys.self)
|
38 | 51 |
|
39 |
| - timestamp = try container.decode(Date.self, forKey: .timestamp) |
| 52 | + let timestamp = try container.decode(Date.self, forKey: .timestamp) |
40 | 53 | let traceIdString = try container.decode(String.self, forKey: .traceId)
|
41 |
| - traceId = SentryId(uuidString: traceIdString) |
42 |
| - level = try container.decode(SentryLog.Level.self, forKey: .level) |
43 |
| - body = try container.decode(String.self, forKey: .body) |
44 |
| - attributes = try container.decode([String: SentryLog.Attribute].self, forKey: .attributes) |
45 |
| - severityNumber = try container.decodeIfPresent(Int.self, forKey: .severityNumber) |
| 54 | + let traceId = SentryId(uuidString: traceIdString) |
| 55 | + let level = try container.decode(Level.self, forKey: .level) |
| 56 | + let body = try container.decode(String.self, forKey: .body) |
| 57 | + let attributes = try container.decode([String: Attribute].self, forKey: .attributes) |
| 58 | + let severityNumber = try container.decodeIfPresent(Int.self, forKey: .severityNumber).map { NSNumber(value: $0) } |
| 59 | + |
| 60 | + self.init( |
| 61 | + timestamp: timestamp, |
| 62 | + traceId: traceId, |
| 63 | + level: level, |
| 64 | + body: body, |
| 65 | + attributes: attributes, |
| 66 | + severityNumber: severityNumber |
| 67 | + ) |
46 | 68 | }
|
47 | 69 |
|
48 |
| - func encode(to encoder: any Encoder) throws { |
| 70 | + @_spi(Private) public func encode(to encoder: any Encoder) throws { |
49 | 71 | var container = encoder.container(keyedBy: CodingKeys.self)
|
50 | 72 |
|
51 | 73 | try container.encode(timestamp, forKey: .timestamp)
|
52 | 74 | try container.encode(traceId.sentryIdString, forKey: .traceId)
|
53 | 75 | try container.encode(level, forKey: .level)
|
54 | 76 | try container.encode(body, forKey: .body)
|
55 | 77 | try container.encode(attributes, forKey: .attributes)
|
56 |
| - try container.encodeIfPresent(severityNumber, forKey: .severityNumber) |
| 78 | + try container.encodeIfPresent(severityNumber?.intValue, forKey: .severityNumber) |
57 | 79 | }
|
58 | 80 | }
|
0 commit comments