Skip to content

Commit

Permalink
Implement log package
Browse files Browse the repository at this point in the history
  • Loading branch information
ns-vasilev committed Oct 18, 2023
1 parent 0653d1a commit ed93533
Show file tree
Hide file tree
Showing 36 changed files with 1,219 additions and 54 deletions.
11 changes: 0 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ jobs:
with:
args: --strict
env:

DIFF_BASE: ${{ github.base_ref }}

Latest:
name: Test Latest (iOS, macOS, tvOS, watchOS)
runs-on: macOS-12
Expand All @@ -40,32 +38,23 @@ jobs:
fail-fast: false
matrix:
include:

- destination: "OS=16.1,name=iPhone 14 Pro"
name: "iOS"
scheme: "Log"
sdk: iphonesimulator


- destination: "OS=16.1,name=Apple TV"
name: "tvOS"
scheme: "Log"
sdk: appletvsimulator


- destination: "OS=9.1,name=Apple Watch Series 8 (45mm)"
name: "watchOS"
scheme: "Log"
sdk: watchsimulator


- destination: "platform=macOS"
name: "macOS"
scheme: "Log"
sdk: macosx

steps:

- uses: actions/checkout@v3
- name: ${{ matrix.name }}
run: xcodebuild test -scheme "${{ matrix.scheme }}" -destination "${{ matrix.destination }}" clean -enableCodeCoverage YES -resultBundlePath "./${{ matrix.sdk }}.xcresult" | xcpretty -r junit
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/danger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@ jobs:
bundle install --clean --path vendor/bundle
- name: danger
env:

DANGER_GITHUB_API_TOKEN: ${{ secrets.DANGER_GITHUB_API_TOKEN }}

run: bundle exec danger --verbose
6 changes: 3 additions & 3 deletions .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Use 'swiftformat --options' to list all of the possible options

--header "\nLog\nCopyright © {created.year} Space Code. All rights reserved.\n//"
--header "\nlog\nCopyright © {created.year} Space Code. All rights reserved.\n//"

--enable blankLinesBetweenScopes
--enable blankLinesAtStartOfScope
Expand Down Expand Up @@ -36,8 +36,8 @@
--enable redundantSelf
--enable redundantVoidReturnType
--enable semicolons
--enable sortedImports
--enable sortedSwitchCases
--enable sortImports
--enable sortSwitchCases
--enable spaceAroundBraces
--enable spaceAroundBrackets
--enable spaceAroundComments
Expand Down
1 change: 0 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ opt_in_rules: # some rules are only opt-in
- closure_end_indentation
- closure_spacing
- collection_alignment
- conditional_returns_on_newline
- contains_over_filter_count
- contains_over_filter_is_empty
- contains_over_first_not_nil
Expand Down
103 changes: 103 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/Log.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Log"
BuildableName = "Log"
BlueprintName = "Log"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "LogTests"
BuildableName = "LogTests"
BlueprintName = "LogTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES"
onlyGenerateCoverageForSpecifiedTargets = "YES"
shouldAutocreateTestPlan = "YES">
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Log"
BuildableName = "Log"
BlueprintName = "Log"
ReferencedContainer = "container:">
</BuildableReference>
</CodeCoverageTargets>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "LogTests"
BuildableName = "LogTests"
BlueprintName = "LogTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Log"
BuildableName = "Log"
BlueprintName = "Log"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# Change Log
All notable changes to this project will be documented in this file.
All notable changes to this project will be documented in this file.

#### 1.x Releases
- `1.0.x` Releases - [1.0.0](#100)

## [1.0.0](https://github.com/space-code/log/releases/tag/1.0.0)
Released on 2023-10-18.

#### Added
- Initial release of log.
- Added by [Nikita Vasilev](https://github.com/nik3212).
32 changes: 12 additions & 20 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
// swift-tools-version: 5.7
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Log",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "Log",
targets: ["Log"]
),
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.watchOS(.v7),
.tvOS(.v13),
.visionOS(.v1),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
products: [
.library(name: "Log", targets: ["Log"]),
],
dependencies: [],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "Log",
dependencies: []
),
.testTarget(
name: "LogTests",
dependencies: ["Log"]
),
.target(name: "Log", dependencies: []),
.testTarget(name: "LogTests", dependencies: ["Log"]),
]
)
24 changes: 24 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.
// swiftlint:disable all

import PackageDescription

let package = Package(
name: "Log",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.watchOS(.v7),
.tvOS(.v13),
],
products: [
.library(name: "Log", targets: ["Log"]),
],
dependencies: [],
targets: [
.target(name: "Log", dependencies: []),
.testTarget(name: "LogTests", dependencies: ["Log"]),
]
)
// swiftlint:enable all
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![Log: A lightweight logging framework written in Swift](https://raw.githubusercontent.com/space-code/log/dev/Resources/log.png)

<h1 align="center" style="margin-top: 0px;">log</h1>

<p align="center">
Expand All @@ -8,7 +10,7 @@
</p>

## Description
`Log` description.
`Log` is a lightweight logging framework written in Swift.

- [Usage](#usage)
- [Requirements](#requirements)
Expand All @@ -20,7 +22,61 @@

## Usage

### Create a logger instance

First, you need to create an instance of `IPrinter` that prints messages to a specific output, such as XCode's console or the `Console` app.
The `log` package provides predefined printers for printing messages in the XCode console (`ConsolePrinter`) and the system console (`OSPrinter`). You can also create your own printer. To do this, your object must conform to `IPrinterStrategy` and implement the necessary methods.

```swift
import Log

let osPrinter = OSPrinter()
let consolePrinter = ConsolePrinter()
```

Second, create a `Logger` instance and pass these printers as initialization parameters while defining a log level. The log level determines the level of log messages to print. If the log level is set to a specific level, all messages with different log levels will be ignored. To print all messages, use `.all`.

```swift
let log = Logger(
printers: [osPrinter, consolePrinter],
logLevel: .all
)
log.error(message: "test message")
```

### Formatting a message

Each instance of `IPrinter` has an array of formatters that are responsible for formatting input messages. The `log` package provides predefined prefix and timestamp formatters. To use these, you need to pass them to an initializer of a printer.

```swift
let osPrinter = OSPrinter(formatters: [PrefixFormatter(name: "your prefix here")])
...
log.fault(message: "message") // "🚨🚨🚨 [your prefix here] => message"
```

Here is a list of predefined formatters:

| **Formatters** | **Description** |
|----------------------------|-------------------------------------------------------------------------------------|
| **PrefixLogFormatter** | Add a specified prefix to a printed message |
| **TimestampLogFormatter** | Add a timestamp before a printed message based on a date format |

### Custom formatters

If you want to create a custom message formatter, your object must conform to the `ILogFormatter` protocol and implement the necessary methods.

```swift
struct MyCustomMessageFormatter: ILogFormatter {
func format(message: String, with logLevel: LogLevel) -> String {
// your implementation here
}
}
```

## Requirements
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 7.0+ / visionOS 1.0+
- Xcode 14.0
- Swift 5.7

## Installation
### Swift Package Manager
Expand Down Expand Up @@ -53,4 +109,4 @@ Please feel free to help out with this project! If you see something that could
Nikita Vasilev, [email protected]

## License
log is available under the MIT license. See the LICENSE file for more info.
log is available under the MIT license. See the LICENSE file for more info.
Binary file added Resources/log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions Sources/Log/Classes/Core/Formatters/Interfaces/ILogFormatter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// log
// Copyright © 2023 Space Code. All rights reserved.
//

import Foundation

/// Specifies the format to be used in the log message
public protocol ILogFormatter {
/// Concatenates the specified attributes and generates the final log message
///
/// - Parameters:
/// - message: A `String` value that contains the message.
/// - logLevel: A `LogLevel` value that contains the logging level.
func format(message: String, with logLevel: LogLevel) -> String
}
Loading

0 comments on commit ed93533

Please sign in to comment.