Skip to content

Commit 6f17763

Browse files
committed
Make the logLevel property changeable (#5)
* Make the `logLevel` property changable * Update `CHANGELOG.md`
1 parent 941be4d commit 6f17763

5 files changed

Lines changed: 73 additions & 5 deletions

File tree

.swiftlint.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ opt_in_rules: # some rules are only opt-in
1616
- attributes
1717
- closure_body_length
1818
- closure_end_indentation
19-
- closure_spacing
2019
- collection_alignment
2120
- contains_over_filter_count
2221
- contains_over_filter_is_empty

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
55
#### Added
6+
- Make the `logLevel` property changeable
7+
- Added in Pull Request [#5](https://github.com/space-code/log/pull/5).
68
- Add files to comply with community standards
79
- Added in Pull Request [#4](https://github.com/space-code/log/pull/4).
810
- Update GitHub Actions workflow

Sources/Log/Classes/Core/Logger/Logger.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
//
22
// log
3-
// Copyright © 2023 Space Code. All rights reserved.
3+
// Copyright © 2024 Space Code. All rights reserved.
44
//
55

6+
import Foundation
7+
68
// MARK: - Logger
79

810
/// A class responsible for logging functionality.
911
open class Logger {
1012
// MARK: Properties
1113

1214
/// The current log level for this logger.
13-
let logLevel: LogLevel
15+
private var _logLevel: Atomic<LogLevel>
16+
17+
/// The current log level for this logger.
18+
public var logLevel: LogLevel {
19+
get { _logLevel.value }
20+
set { _logLevel.value = newValue }
21+
}
22+
1423
/// An array of printer strategies to handle the log output.
1524
let printers: [IPrinterStrategy]
1625

@@ -26,7 +35,7 @@ open class Logger {
2635
logLevel: LogLevel
2736
) {
2837
self.printers = printers
29-
self.logLevel = logLevel
38+
_logLevel = Atomic(value: logLevel)
3039
}
3140

3241
// MARK: Private
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// log
3+
// Copyright © 2024 Space Code. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
// MARK: - Atomic
9+
10+
/// The Atomic class is designed to provide thread-safe access to a mutable value.
11+
final class Atomic<Value> {
12+
// MARK: Properties
13+
14+
/// NSLock instance for synchronization.
15+
private let lock = NSLock()
16+
/// The actual mutable value.
17+
private var _value: Value
18+
19+
/// Computed property to get and set the value atomically
20+
var value: Value {
21+
get { lock.synchronized { _value } }
22+
set { lock.synchronized { _value = newValue }}
23+
}
24+
25+
// MARK: Initialization
26+
27+
/// Initializes the Atomic instance with an initial value.
28+
init(value: Value) {
29+
_value = value
30+
}
31+
}
32+
33+
// MARK: - Extensions
34+
35+
private extension NSLock {
36+
/// Synchronizes a code block using the NSLock instance.
37+
/// This helps ensure that only one thread can access the critical section at a time.
38+
func synchronized<T>(block: () throws -> T) rethrows -> T {
39+
lock()
40+
defer { unlock() }
41+
return try block()
42+
}
43+
}

Tests/LogTests/UnitTests/LogTests.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// log
3-
// Copyright © 2023 Space Code. All rights reserved.
3+
// Copyright © 2024 Space Code. All rights reserved.
44
//
55

66
import Log
@@ -119,6 +119,21 @@ final class LogTests: XCTestCase {
119119
XCTAssertNil(printerMock.invokedLogParameters?.message)
120120
}
121121

122+
func test_thatLoggerDoesNotPrintAnything_whenLogLevelValueDidChange() {
123+
// given
124+
let sut = prepareSut()
125+
126+
// when
127+
sut.logLevel = .info
128+
sut.debug(message: .message)
129+
sut.info(message: .message)
130+
131+
// then
132+
XCTAssertEqual(sut.logLevel, .info)
133+
XCTAssertEqual(printerMock.invokedLogCount, 1)
134+
XCTAssertEqual(printerMock.invokedLogParameters?.message, .message)
135+
}
136+
122137
// MARK: Private
123138

124139
private func prepareSut(logLevel: LogLevel = .all) -> Logger {

0 commit comments

Comments
 (0)