Skip to content

Commit 6e9c6cc

Browse files
Make configuration a little less dependant on Sentry being set up (#104)
* move sentry config options into their own struct make sentry integration more optional * move sentry config outside of other config object * missed removing sentryconfig from config constructor * add attach function to add custom log destinations * add trace sample rate to sentry config * add readme info for traces sample rate
1 parent fe18afd commit 6e9c6cc

File tree

6 files changed

+143
-62
lines changed

6 files changed

+143
-62
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## Un-released
7+
## [2.0.0]
8+
- Split Sentry configuration into a separate, optional struct to make not using Sentry a little easier
89
- Updated LogLevelPreset names and mapped LogLevels (#91)
910

1011
## [0.5.4]

README.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,52 @@ _Firebase Crashlytics is no longer a supported destination for crash reporting_
5050

5151
SteamcLog has a number of configuration options
5252

53-
### logLevel: LogLevelPreset
53+
#### logLevel: LogLevelPreset
5454
Destination logging levels; it is recommended to use the defaults set by Steamclog instead of initializing these manually. In special cases where more data is desired, update this property. See technical documentation for more details on the available presets.
5555

56-
### requireRedacted: Bool
56+
#### requireRedacted: Bool
5757
Default value is `false`.
5858
Require that all logged objects conform to Redacted or are all redacted by default.
5959

60-
### autoRotateConfig: AutoRotateConfig
60+
#### autoRotateConfig: AutoRotateConfig
6161
By default, logs will rotate every 10 minutes, and store 10 archived log files.
6262
`AutoRotateConfig` allows customization for the auto-rotating behaviour.
6363

6464
`AutoRotateConfig` has the following fields:
6565
**fileRotationTime: TimeInterval**: The number of seconds before the log file is rotated and archived.
6666

67-
### sentryFilter: SentryFilter
67+
Additionally, SteamcLog comes with support to log to Sentry:
68+
69+
### Sentry configuration options
70+
71+
#### key: String
72+
Your Sentry key
73+
74+
#### attachStackTrace: Bool
75+
Default value is `true`.
76+
Toggles Sentry automatically attaching stack traces to error reports.
77+
78+
#### autoSessionTracking: Bool
79+
Default value is `true`.
80+
Toggle's Sentry's auto session tracking. More info [here](https://docs.sentry.io/platforms/cocoa/?platform=swift#release-health).
81+
82+
#### debug: Bool
83+
Default value is `false`.
84+
Sets Sentry to debug mode. More info [here](https://docs.sentry.io/error-reporting/configuration/?platform=swift#debug)
85+
86+
#### tracesSampleRate: NSNumber
87+
Default value is 0.0.
88+
Sets the percentage of the tracing data that is collected by Sentry. Values must be between 0 and 1, and values larger than 1 will be set to 1.
89+
Note that setting this to anything greater than 0 can cause projects to blow past their usage quotas by generating far more events than normal.
90+
91+
#### filter: SentryFilter
6892
By default, all error objects will be sent to Sentry when submitted via the `error` call.
69-
`SentryFilter` allows you to change this behaviour at the SteamcLog Config-level.
93+
This allows you to change this behaviour at the SteamcLog Config-level, by passing in a function that filters errors from being logged.
7094

7195
```swift
72-
Config(
96+
SentryConfig(
7397
// other fields
74-
sentryFilter: { error in
98+
filter: { error in
7599
if let error = error as? CustomError {
76100
return true // CustomError errors will no longer be submitted to Sentry
77101
}
@@ -125,6 +149,10 @@ And the log will output:
125149

126150
`User(name: "Name", uuid: <redacted>, email: "hi@steamclock.com", created: <redacted>)`
127151

152+
## Custom Log Destinations
153+
154+
In addition to the Sentry log destination that comes packaged with SteamcLog, you can create your own log destination and attach it to your SteamcLog instance using `attach`.
155+
128156
## Using SteamcLog with Netable
129157

130158
If you're also using [Netable](https://github.com/steamclock/netable), you can pipe your logs directly from Netable into SteamcLog.

SteamcLog.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'SteamcLog'
11-
s.version = '1.0.1'
11+
s.version = '2.0.0'
1212
s.summary = 'A short description of SteamcLog.'
1313

1414
# This description is used to generate tags and improve search results.

SteamcLog/Classes/Helper/Config.swift

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,23 @@ public struct Config {
2121
/// Require that all logged objects conform to Redacted or are all redacted by default.
2222
@usableFromInline internal let requireRedacted: Bool
2323

24-
/// Sentry project specific key. Found here: https://docs.sentry.io/platforms/cocoa/?platform=swift
25-
/// Set this key to an empty string to not report logs to Sentry.
26-
internal let sentryKey: String
27-
28-
/// Debug mode for Sentry SDK. Default is false.
29-
/// More info here: https://docs.sentry.io/error-reporting/configuration/?platform=swift#debug
30-
internal let sentryDebug: Bool
31-
32-
/// Toggles Sentry auto session tracking. Default is false.
33-
/// More info here: https://docs.sentry.io/platforms/cocoa/?platform=swift#release-health
34-
internal let sentryAutoSessionTracking: Bool
35-
36-
/// Toggles Sentry attaching stack traces to errors. Default is true.
37-
internal let sentryAttachStacktrace: Bool
38-
39-
/// Toggles the ability to filter out errors from being reported to Sentry
40-
internal let sentryFilter: SentryFilter
41-
4224
/*
4325
* Create a new SteamcLog configuration to use.
4426
*
4527
* - Parameters:
46-
* - sentryKey: Sentry project key, needed to initialize SentrySDK.
4728
* - logLevel: The log level presets for each destination. Default is `.develop`.
4829
* - requireRedacted: If true, all logged objects must conform to `Redacted` or be redacted by default. Default is false.
4930
* - identifier: The indentifier to note logs under. Default is "steamclog".
5031
* - autoRotateConfig: Customize when logs are rotated. Defaults to 600 seconds.
51-
* - sentryDebug: Enable debug mode for SentrySDK. Default is false.
52-
* - sentryAutoSessionTracking: Enably SentrySDK auto session tracking. Default is false.
5332
*/
5433
public init(
55-
sentryKey: String,
5634
logLevel: LogLevelPreset = .debug,
5735
requireRedacted: Bool = false,
5836
identifier: String = "steamclog",
59-
autoRotateConfig: AutoRotateConfig = AutoRotateConfig(),
60-
sentryDebug: Bool = false,
61-
sentryAutoSessionTracking: Bool = true,
62-
sentryAttachStacktrace: Bool = true,
63-
sentryFilter: @escaping SentryFilter = { error in false }) {
37+
autoRotateConfig: AutoRotateConfig = AutoRotateConfig()) {
6438
self.requireRedacted = requireRedacted
6539
self.logLevel = logLevel
6640
self.identifier = identifier
6741
self.autoRotateConfig = autoRotateConfig
68-
self.sentryKey = sentryKey
69-
self.sentryDebug = sentryDebug
70-
self.sentryAutoSessionTracking = sentryAutoSessionTracking
71-
self.sentryAttachStacktrace = sentryAttachStacktrace
72-
self.sentryFilter = sentryFilter
7342
}
7443
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// SentryConfig.swift
3+
//
4+
//
5+
// Created by Brendan on 2022-03-21.
6+
//
7+
8+
import Foundation
9+
10+
public struct SentryConfig {
11+
/// Sentry project specific key. Found here: https://docs.sentry.io/platforms/cocoa/?platform=swift
12+
/// Set this key to an empty string to not report logs to Sentry.
13+
let key: String
14+
15+
/// Toggles Sentry attaching stack traces to errors. Default is true.
16+
let attachStackTrace: Bool
17+
18+
/// Toggles Sentry auto session tracking. Default is false.
19+
/// More info here: https://docs.sentry.io/platforms/cocoa/?platform=swift#release-health
20+
let autoSessionTracking: Bool
21+
22+
/// Debug mode for Sentry SDK. Default is false.
23+
/// More info here: https://docs.sentry.io/error-reporting/configuration/?platform=swift#debug
24+
let debug: Bool
25+
26+
/// Sets the percentage of the tracing data that is collected by Sentry. Default is 0.
27+
let tracesSampleRate: NSNumber
28+
29+
/// Toggles the ability to filter out errors from being reported to Sentry
30+
let filter: SentryFilter
31+
32+
/*
33+
* Create a new Sentry Configuration to use.
34+
*
35+
* - Parameters:
36+
* - key: Sentry project key, needed to initialize SentrySDK.
37+
* - attachStackTrace: Toggles Sentry attaching stack traces to errors. Default is true.
38+
* - autoSessionTracking: Enable SentrySDK auto session tracking. Default is false.
39+
* - debug: Enable debug mode for SentrySDK. Default is false.
40+
* - tracesSampleRate: Sets the percentage of the tracing data that is collected by Sentry.
41+
* - filter: Toggles the ability to filter out errors from being reported to Sentry
42+
*/
43+
public init(
44+
key: String,
45+
attachStacktrace: Bool = true,
46+
autoSessionTracking: Bool = true,
47+
debug: Bool = false,
48+
tracesSampleRate: NSNumber = 0.0,
49+
filter: @escaping SentryFilter = { error in false }) {
50+
self.key = key
51+
self.attachStackTrace = attachStacktrace
52+
self.autoSessionTracking = autoSessionTracking
53+
self.debug = debug
54+
self.tracesSampleRate = tracesSampleRate
55+
self.filter = filter
56+
}
57+
}

SteamcLog/Classes/SteamcLog.swift

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,59 @@ import XCGLogger
1313
public struct SteamcLog {
1414
public var config: Config! {
1515
didSet {
16-
sentryDestination.outputLevel = config.logLevel.remote.xcgLevel
16+
sentryDestination?.outputLevel = config.logLevel.remote.xcgLevel
1717
fileDestination.outputLevel = config.logLevel.disk.xcgLevel
1818
systemDestination.outputLevel = config.logLevel.console.xcgLevel
1919
}
2020
}
2121

22+
// Sentry configuration options. If this is not set (or set to nil during creation), Sentry will not be used.
23+
public var sentryConfig: SentryConfig?
24+
2225
@usableFromInline internal var xcgLogger: XCGLogger!
2326
@usableFromInline internal let encoder = JSONEncoder()
2427

2528
private var fileDestination: FileDestination!
26-
private var sentryDestination: SentryDestination!
29+
private var sentryDestination: SentryDestination?
2730
private var systemDestination: SteamcLogSystemLogDestination!
2831

29-
public init(_ config: Config) {
32+
/*
33+
* Create your SteamcLog object.
34+
* We recommend setting up a global instance of SteamcLog, probably in your AppDelegate file.
35+
*
36+
* - Parameters:
37+
* - config: General configuration options. See Config.swift for details.
38+
* - sentryConfig: Sentry configuration options. If this is `nil`, Sentry will not be used.
39+
*/
40+
public init(_ config: Config, sentryConfig: SentryConfig?) {
3041
self.config = config
3142
xcgLogger = XCGLogger(identifier: config.identifier, includeDefaultDestinations: false)
3243

3344
xcgLogger.setup(
3445
level: config.logLevel.global.xcgLevel
3546
)
3647

37-
SentrySDK.configureScope { scope in
38-
#if DEBUG
39-
scope.setTag(value: "debug", key: "environment")
40-
#else
41-
scope.setTag(value: "prod", key: "environment")
42-
#endif
43-
}
48+
if let sentry = sentryConfig {
49+
SentrySDK.configureScope { scope in
50+
#if DEBUG
51+
scope.setTag(value: "debug", key: "environment")
52+
#else
53+
scope.setTag(value: "prod", key: "environment")
54+
#endif
55+
}
4456

45-
SentrySDK.start { options in
46-
options.dsn = config.sentryKey
47-
options.debug = config.sentryDebug
48-
options.attachStacktrace = config.sentryAttachStacktrace
49-
options.enableAutoSessionTracking = config.sentryAutoSessionTracking
50-
options.tracesSampleRate = 0.0
51-
}
57+
SentrySDK.start { options in
58+
options.dsn = sentry.key
59+
options.debug = sentry.debug
60+
options.attachStacktrace = sentry.attachStackTrace
61+
options.enableAutoSessionTracking = sentry.autoSessionTracking
62+
options.tracesSampleRate = sentry.tracesSampleRate
63+
}
5264

53-
sentryDestination = SentryDestination(identifier: "steamclog.sentryDestination")
54-
setLoggingDetails(destination: &sentryDestination, outputLevel: config.logLevel.remote)
55-
xcgLogger.add(destination: sentryDestination)
65+
sentryDestination = SentryDestination(identifier: "steamclog.sentryDestination")
66+
setLoggingDetails(destination: &sentryDestination!, outputLevel: config.logLevel.remote)
67+
xcgLogger.add(destination: sentryDestination! )
68+
}
5669

5770
fileDestination = AutoRotatingFileDestination(writeToFile: logFilePath,
5871
identifier: "steamclog.fileDestination",
@@ -91,6 +104,19 @@ public struct SteamcLog {
91104

92105
// MARK: - Public Methods
93106

107+
/*
108+
* Attach a new custom log destination to the current logger
109+
*
110+
* - Parameters:
111+
* - destination: The new destination to attach to the logger.
112+
* - outputLevel: The minimum log level that will be sent to the destination.
113+
*/
114+
public func attach(destination: BaseQueuedDestination, outputLevel: LogLevel) {
115+
var newDest = destination
116+
setLoggingDetails(destination: &newDest, outputLevel: outputLevel)
117+
xcgLogger.add(destination: newDest)
118+
}
119+
94120
// MARK: All Log Level
95121

96122
private func internalVerbose(_ message: String, functionName: StaticString, fileName: StaticString, lineNumber: Int) {
@@ -272,7 +298,7 @@ public struct SteamcLog {
272298

273299
public func error<T>(_ message: StaticString, _ object: T, functionName: StaticString = #function, fileName: StaticString = #file, lineNumber: Int = #line) where T: Encodable {
274300

275-
if let error = object as? Error, config.sentryFilter(error) {
301+
if let error = object as? Error, let sentry = sentryConfig, sentry.filter(error) {
276302
info("\(error) included in sentryFilter and has been blocked from being captured as an error: \(error.localizedDescription)",
277303
functionName: functionName,
278304
fileName: fileName,

0 commit comments

Comments
 (0)