Skip to content

Commit

Permalink
Added SDK testlog (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
blaubaer authored Jul 25, 2024
1 parent e5d7703 commit 956da05
Show file tree
Hide file tree
Showing 41 changed files with 1,389 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
name: Test
strategy:
matrix:
go-version: [ 1.17.0, 1.20.0, 1.21.0 ]
go-version: [ 1.17.0, 1.21.0, 1.22.0 ]
os: [ ubuntu-latest, macos-latest, windows-latest ]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 echocat
Copyright (c) 2020-2024 echocat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ Done. Enjoy!
## Implementations
### [slf4g-native](native)
1. [native](native): Reference implementation of [slf4g](https://github.com/echocat/slf4g).
This is the reference implementation of [slf4g](https://github.com/echocat/slf4g).
2. [testlog](sdk/testlog): Ensure that everything which is logged within test by [slf4g](https://github.com/echocat/slf4g) appears correctly within tests.
2. [recording](testing/recording): Will record everything which is logged by [slf4g](https://github.com/echocat/slf4g) and can then be asserted inside test cases.
## Contributing
Expand Down
54 changes: 27 additions & 27 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
// The Simple Logging Facade for Go provides an easy access who everyone who
// wants to log something and do not want to care how it is logged and gives
// others the possibility to implement their own loggers in easy way.
// Package log is the Simple Logging Facade for Go provides an easy access who
// everyone who wants to log something and do not want to care how it is logged
// and gives others the possibility to implement their own loggers in easy way.
//
// Usage
// # Usage
//
// There are 2 common ways to use this framework.
//
// 1. By getting a logger for your current package. This is the most common way
// and is quite clean for one package. If the package contains too many logic
// it might worth it to use the 2nd approach (see below).
//
// package foo
// package foo
//
// import "github.com/echocat/slf4g"
// import "github.com/echocat/slf4g"
//
// var logger = log.GetLoggerForCurrentPackage()
// var logger = log.GetLoggerForCurrentPackage()
//
// func sayHello() {
// // will log with logger="github.com/user/foo"
// logger.Info("Hello, world!")
// }
// func sayHello() {
// // will log with logger="github.com/user/foo"
// logger.Info("Hello, world!")
// }
//
// 2. By getting a logger for the object the logger is for. This is the most
// clean approach and will give you later the maximum flexibility and control.
//
// package foo
// package foo
//
// import "github.com/echocat/slf4g"
// import "github.com/echocat/slf4g"
//
// var logger = log.GetLogger(myType{})
// var logger = log.GetLogger(myType{})
//
// type myType struct {
// ...
// }
// type myType struct {
// ...
// }
//
// func (mt myType) sayHello() {
// // will log with logger="github.com/user/foo.myType"
// logger.Info("Hello, world!")
// }
// func (mt myType) sayHello() {
// // will log with logger="github.com/user/foo.myType"
// logger.Info("Hello, world!")
// }
//
// 3. By using the global packages methods which is quite equal to how the SDK
// base logger works. This is only recommend for small application and not for
// libraries you like to export.
//
// package foo
// package foo
//
// import "github.com/echocat/slf4g"
// import "github.com/echocat/slf4g"
//
// func sayHello() {
// // will log with logger=ROOT
// log.Info("Hello, world!")
// }
// func sayHello() {
// // will log with logger=ROOT
// log.Info("Hello, world!")
// }
package log
3 changes: 2 additions & 1 deletion fields/filtered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package fields

import (
"fmt"
"github.com/echocat/slf4g/level"
"testing"

"github.com/echocat/slf4g/level"

"github.com/echocat/slf4g/internal/test/assert"
)

Expand Down
1 change: 1 addition & 0 deletions native/color/prepare_appengine.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !mock && appengine
// +build !mock,appengine

package color
Expand Down
1 change: 1 addition & 0 deletions native/color/prepare_bsd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build (!mock && darwin) || dragonfly || freebsd || netbsd || openbsd
// +build !mock,darwin dragonfly freebsd netbsd openbsd

package color
Expand Down
1 change: 1 addition & 0 deletions native/color/prepare_mock.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build mock
// +build mock

package color
Expand Down
1 change: 1 addition & 0 deletions native/color/prepare_no_terminal.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build (!mock && js) || nacl || plan9
// +build !mock,js nacl plan9

package color
Expand Down
1 change: 1 addition & 0 deletions native/color/prepare_notappengine.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !mock && !appengine && !js && !windows && !nacl && !plan9
// +build !mock,!appengine,!js,!windows,!nacl,!plan9

package color
Expand Down
1 change: 1 addition & 0 deletions native/color/prepare_solaris.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !mock
// +build !mock

package color
Expand Down
1 change: 1 addition & 0 deletions native/color/prepare_unix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build (!mock && linux) || aix
// +build !mock,linux aix

package color
Expand Down
1 change: 1 addition & 0 deletions native/color/prepare_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !mock
// +build !mock

package color
Expand Down
1 change: 1 addition & 0 deletions native/color/supported_mocked_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build mock
// +build mock

package color
Expand Down
36 changes: 19 additions & 17 deletions native/doc.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
// This is the reference implementation of a logger of the slf4g framework
// Package native holds the reference implementation of a logger of the slf4g framework
// (https://github.com/echocat/slf4g).
//
// Usage
// # Usage
//
// For the most common cases it is fully enough to anonymously import this
// package in your main.go; nothing more is needed.
//
// github.com/foo/bar/main/main.go:
// package main
//
// import (
// "github.com/foo/bar"
// _ "github.com/echocat/slf4g/native"
// )
// package main
//
// func main() {
// bar.SayHello()
// }
// import (
// "github.com/foo/bar"
// _ "github.com/echocat/slf4g/native"
// )
//
// func main() {
// bar.SayHello()
// }
//
// github.com/foo/bar/bar.go:
// package bar
//
// import (
// "github.com/echocat/slf4g"
// )
// package bar
//
// import (
// "github.com/echocat/slf4g"
// )
//
// func SayHello() {
// log.Info("Hello, world!")
// }
// func SayHello() {
// log.Info("Hello, world!")
// }
//
// See more useful stuff in the examples sections.
package native
13 changes: 8 additions & 5 deletions native/facade/value/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
// encoding.TextUnmarshaler, too.
//
// Example:
// pv := value.NewProvider(native.DefaultProvider)
//
// flag.Var(pv.Consumer.Formatter, "log.format", "Configures the log format.")
// flag.Var(pv.Level, "log.level", "Configures the log level.")
// pv := value.NewProvider(native.DefaultProvider)
//
// flag.Var(pv.Consumer.Formatter, "log.format", "Configures the log format.")
// flag.Var(pv.Level, "log.level", "Configures the log level.")
//
// flag.Parse()
//
// flag.Parse()
// Now you can call:
// $ <myExecutable> -log.format=json -log.level=debug ...
//
// $ <myExecutable> -log.format=json -log.level=debug ...
package value
1 change: 1 addition & 0 deletions native/facade/value/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package value

import (
"fmt"

"github.com/echocat/slf4g/native/color"

"github.com/echocat/slf4g/native/formatter"
Expand Down
4 changes: 2 additions & 2 deletions native/formatter/functions/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func Test_Colorize(t *testing.T) {
givenColorCode: "15;1",
givenText: "hello, world",
shouldColorize: true,
expected: `[15;1mhello, world[0m`,
expected: `[15;1mhello, world[0m`,
}, {
givenColorCode: "1",
givenText: "hello, world",
shouldColorize: true,
expected: `[1mhello, world[0m`,
expected: `[1mhello, world[0m`,
}, {
givenColorCode: "15;1",
givenText: "hello, world",
Expand Down
1 change: 1 addition & 0 deletions native/formatter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package formatter

import (
"fmt"

"github.com/echocat/slf4g/level"

"github.com/echocat/slf4g/native/execution"
Expand Down
4 changes: 2 additions & 2 deletions native/level/colorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
var DefaultColorizer Colorizer = ColorizerMap{
level.Trace: ``,
level.Debug: ``,
level.Info: `[34;1m`,
level.Warn: `[33;1m`,
level.Info: `[34;1m`,
level.Warn: `[33;1m`,
level.Error: ``,
level.Fatal: ``,
}
Expand Down
8 changes: 4 additions & 4 deletions sdk/bridge/doc.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Package sdk/bridge provides methods to either hook into the SDK logger itself
// or create compatible instances.
//
// Hooks
// # Hooks
//
// The simples way is to simply anonymously import the hook package to configure
// the whole application to use the slf4g framework on any usage of the SDK
// based loggers.
//
// import (
// _ "github.com/echocat/slf4g/sdk/bridge/hook"
// )
// import (
// _ "github.com/echocat/slf4g/sdk/bridge/hook"
// )
//
// For manual hooks please see the examples.
package sdk
6 changes: 3 additions & 3 deletions sdk/bridge/hook/doc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Importing this package anonymously will configure the whole application to
// use the slf4g framework on any usage of the SDK based loggers.
//
// import (
// _ "github.com/echocat/slf4g/sdk/bridge/hook"
// )
// import (
// _ "github.com/echocat/slf4g/sdk/bridge/hook"
// )
package hook
6 changes: 3 additions & 3 deletions sdk/bridge/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// its root Logger and logs everything printed using Print(), Printf() and
// Println() on level.Info.
//
// Limitations
// # Limitations
//
// 1# Stuff logged using Fatal*() and Panic*() are on logged on the same Level
// as everything else.
Expand All @@ -28,7 +28,7 @@ func Configure(customizer ...func(*log.LoggingWriter)) {
// it's the given Logger and logs everything printed using Print(), Printf() and
// Println() on given level.Level.
//
// Limitations
// # Limitations
//
// 1# Stuff logged using Fatal*() and Panic*() are on logged on the same Level
// as everything else.
Expand All @@ -54,7 +54,7 @@ func ConfigureWith(target log.CoreLogger, logAs level.Level, customizer ...func(
// logs everything printed using Print(), Printf() and Println() on given
// level.Level.
//
// Limitations
// # Limitations
//
// 1# Stuff logged using Fatal*() and Panic*() are on logged on the same Level
// as everything else.
Expand Down
31 changes: 31 additions & 0 deletions sdk/testlog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[![PkgGoDev](https://pkg.go.dev/badge/github.com/echocat/slf4g/sdk/testlog)](https://pkg.go.dev/github.com/echocat/slf4g/sdk/testlog)

# slf4g testing Logger implementation

Provides a Logger which will be connected to [`testing.T.Log()`](https://pkg.go.dev/testing#T.Log) of the go SDK.

If you're looking for an instance to record all logged events see [`github.com/echocat/slf4g/testing/recording`](../../testing/recording) package.

## Usage

The easiest way to enable the slf4g framework in your tests is, simply:

```golang
package foo

import (
"testing"
"github.com/echocat/slf4g"
"github.com/echocat/slf4g/sdk/testlog"
)

func TestMyGreatStuff(t *testing.T) {
testlog.Hook(t)

log.Info("Yeah! This is a log!")
}
```

... that's it!

See [`Hook(..)`](hook.go) for more details.
Loading

0 comments on commit 956da05

Please sign in to comment.