Reorg Repository and Start Documentation (#2)

* Reorg Repository and Start Documentation

* Update Github Actions

* Update Copyright and test framework
This commit is contained in:
Justin Hammond 2021-09-22 16:46:38 +08:00 committed by GitHub
parent 74ddd4aeb3
commit f178594beb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 683 additions and 119 deletions

View file

@ -13,7 +13,7 @@ jobs:
strategy: strategy:
matrix: matrix:
go-version: [ 1.16.x, 1.15.x, 1.14.x, 1.13.x ] go-version: [ 1.16.x, 1.15.x, 1.14.x, 1.13.x ]
platform: [ ubuntu-latest, macos-latest ] platform: [ ubuntu-latest, macos-latest, windows-latest ]
runs-on: ${{ matrix.platform }} runs-on: ${{ matrix.platform }}
steps: steps:
- name: Install Go - name: Install Go
@ -24,12 +24,38 @@ jobs:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v1 uses: actions/checkout@v1
- name: Run tests - name: Run tests
run: go test -v -race -covermode=atomic -coverprofile=coverage.out run: go test -v -race ./...
- name: CodeCov codecov:
uses: codecov/codecov-action@v2
goreadme:
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: test needs: test
steps:
- name: Install Go
if: success()
uses: actions/setup-go@v1
with:
go-version: 1.16.x
- name: Checkout code
uses: actions/checkout@v1
- name: Run tests
run: go mod tidy && go test -v -race -covermode=atomic -coverprofile=coverage.out ./...
- name: CodeCov
uses: codecov/codecov-action@v2
lint:
name: Lint project using GolangCI Lint
runs-on: ubuntu-latest
needs: test
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: GolangCI-Lint Action
uses: golangci/golangci-lint-action@v2.5.2
with:
version: latest
only-new-issues: true
args: --issues-exit-code=0
goreadme:
runs-on: ubuntu-latest
needs: [codecov, lint]
steps: steps:
- name: Check out repository - name: Check out repository
uses: actions/checkout@v2 uses: actions/checkout@v2

View file

@ -1,16 +0,0 @@
on: [ push, pull_request ]
name: Linter
jobs:
lint:
name: Lint project using GolangCI Lint
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: GolangCI-Lint Action
uses: golangci/golangci-lint-action@v2.5.2
with:
version: latest
only-new-issues: true
args: --issues-exit-code=0

View file

@ -1,6 +1,5 @@
MIT License MIT License
Copyright (c) 2021 Sherif Abdel-Naby
Copyright (c) 2021 Justin Hammond Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy

57
doc.go Normal file
View file

@ -0,0 +1,57 @@
/*
MIT License
Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
Package go-logadapter allows you to use different Log Libraries in your
packages/applications
This package just provides wrappers around different log libraries and
allows the user of your library to use their preferred Logging Library
It implements common Logging Levels:
* Trace
* Debug
* Info
* Warn
* Error
* Fatal
* Panic
It also supports basic structured logging features, but specifing a key/value
pair using the With Statement.
Supported Logging Libraries
logadapter currently supports:
* (std library logger) https://pkg.go.dev/log
* (logrus) https://github.com/sirupsen/logrus
* (zap) https://github.com/uber-go/zap
Adding additional Logging Libraries is realatively straight forward by creating
a wrapper that implements the (Logger) https://pkg.go.dev/github.com/Fishwaldo/go-logadapter#Logger Interface
*/
package logadapter

4
go.mod
View file

@ -3,7 +3,11 @@ module github.com/Fishwaldo/go-logadapter
go 1.16 go 1.16
require ( require (
github.com/kr/pretty v0.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.8.1 github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.19.1 go.uber.org/zap v1.19.1
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
) )

12
go.sum
View file

@ -3,13 +3,15 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
@ -43,8 +45,9 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
@ -56,8 +59,9 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -1,3 +1,27 @@
/*
MIT License
Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package utils package utils
type CopyableMap map[string]interface{} type CopyableMap map[string]interface{}

View file

@ -0,0 +1,72 @@
/*
MIT License
Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package utils
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestCopyMap(t *testing.T) {
m1 := map[string]interface{}{
"a": "bbb",
"b": map[string]interface{}{
"c": 123,
},
"c": []interface{} {
"d", "e", map[string]interface{} {
"f": "g",
},
},
}
m2 := CopyableMap(m1).DeepCopy()
m1["a"] = "zzz"
delete(m1, "b")
m1["c"].([]interface{})[1] = "x"
m1["c"].([]interface{})[2].(map[string]interface{})["f"] = "h"
require.Equal(t, map[string]interface{}{
"a": "zzz",
"c": []interface{} {
"d", "x", map[string]interface{} {
"f": "h",
},
},
}, m1)
require.Equal(t, map[string]interface{}{
"a": "bbb",
"b": map[string]interface{}{
"c": 123,
},
"c": []interface{} {
"d", "e", map[string]interface{} {
"f": "g",
},
},
}, m2)
}

60
logadapter.go Normal file
View file

@ -0,0 +1,60 @@
/*
MIT License
Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package logadapter
import (
)
// Logger is a interface that Applications/Libraries use
//
// The actual Loggers should implement a structure
// that Adhears to this Interface
type Logger interface {
// Log a Trace Message
Trace(message string, params ...interface{})
// Log a Debug Message
Debug(message string, params ...interface{})
// Log a Info Message
Info(message string, params ...interface{})
// Log a Warn Message
Warn(message string, params ...interface{})
// Log a Error Message
Error(message string, params ...interface{})
// Log a Fatal Message (some implementations may call os.exit() here)
Fatal(message string, params ...interface{})
// Log a Panic Message (some implmentations may call Panic)
Panic(message string, params ...interface{})
// Create a New Logger Instance with Name
New(name string) (l Logger)
// Add Key/Value Pairs for Structured Logging and return a new Logger
With(key string, value interface{}) (l Logger)
// Set the Log Prefix
SetPrefix(name string)
// Get the Log Prefix
GetPrefix() (string)
// Sync/Flush the Log Buffers
Sync()
}

View file

@ -1,7 +1,32 @@
/*
MIT License
Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package logadapter_test package logadapter_test
import ( import (
"github.com/Fishwaldo/go-logadapter" "github.com/Fishwaldo/go-logadapter"
"github.com/Fishwaldo/go-logadapter/loggers/std"
) )
type TestStruct struct { type TestStruct struct {
@ -9,8 +34,8 @@ type TestStruct struct {
} }
func (t *TestStruct) Init() { func (t *TestStruct) Init() {
temp := logadapter.DefaultLogger() temp := stdlogger.DefaultLogger()
temp.SetLevel(logadapter.LOG_TRACE) temp.SetLevel(stdlogger.LOG_TRACE)
t.Logger = temp t.Logger = temp
} }

View file

@ -1,4 +1,28 @@
package logruslog /*
MIT License
Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package logrus
import ( import (
"fmt" "fmt"

View file

@ -0,0 +1,138 @@
/*
MIT License
Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package logrus_test
import (
// "bytes"
// "os"
// "regexp"
"testing"
// "github.com/Fishwaldo/go-logadapter/loggers/zap"
)
func TestDefaultLogger(t *testing.T) {
//_ := zaplog.ZapLogger{}
//logger.SetLevel(stdlogger.LOG_TRACE)
//if logger.GetLevel() != stdlogger.LOG_TRACE {
// t.Errorf("Can't Set Logging Level")
//}
}
// func captureOutput(l *zaplog.ZapLogger, f func()) string {
// // var buf bytes.Buffer
// // l.Log.SetOutput(&buf)
// // f()
// // l.Log.SetOutput(os.Stderr)
// // return buf.String()
// }
func TestLogTrace(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// output := captureOutput(logger, func() {
// logger.Trace("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* TRACE: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Trace Failed: %s", output)
// }
}
func TestLogDebug(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// output := captureOutput(logger, func() {
// logger.Debug("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* DEBUG: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Debug Failed: %s", output)
// }
}
func TestLogInfo(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// output := captureOutput(logger, func() {
// logger.Info("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* INFO: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Info Failed: %s", output)
// }
}
func TestLogWarn(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// output := captureOutput(logger, func() {
// logger.Warn("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* WARN: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Warn Failed: %s", output)
// }
}
func TestLogError(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// output := captureOutput(logger, func() {
// logger.Error("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* ERROR: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Error Failed: %s", output)
// }
}
func TestLogFatal(t *testing.T) {
//logger := DefaultLogger()
//logger.SetLevel(LOG_TRACE)
//output := captureOutput(logger, func() {
// logger.Fatal("Hello %s", "world")
//})
//validmsg := regexp.MustCompile(`^.* FATAL: Hello world \- {}`)
//if !validmsg.MatchString(output) {
// t.Errorf("Log Fatal Failed: %s", output)
//}
}
func TestLogPanic(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// defer func() {
// if err := recover(); err == nil {
// t.Errorf("Log Panic Recovery Failed")
// }
// }()
// output := captureOutput(logger, func() {
// logger.Panic("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* PANIC: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Panic Failed: %s", output)
// }
}

View file

@ -1,31 +1,41 @@
package logadapter /*
MIT License
Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package stdlogger
import ( import (
"encoding/json"
"fmt"
"log" "log"
"sync"
"os" "os"
"sync"
"fmt"
"strings" "strings"
"encoding/json"
"github.com/Fishwaldo/go-logadapter/utils" "github.com/Fishwaldo/go-logadapter"
"github.com/Fishwaldo/go-logadapter/internal/utils"
) )
type Logger interface {
Trace(message string, params ...interface{})
Debug(message string, params ...interface{})
Info(message string, params ...interface{})
Warn(message string, params ...interface{})
Error(message string, params ...interface{})
Fatal(message string, params ...interface{})
Panic(message string, params ...interface{})
New(name string) (l Logger)
With(key string, value interface{}) (l Logger)
SetPrefix(name string)
GetPrefix() (string)
Sync()
}
//DefaultLogger uses Golang Standard Logging Libary //DefaultLogger uses Golang Standard Logging Libary
func DefaultLogger() (l *StdLogger) { func DefaultLogger() (l *StdLogger) {
stdlogger := log.New(os.Stderr, "log - ", log.LstdFlags) stdlogger := log.New(os.Stderr, "log - ", log.LstdFlags)
@ -83,7 +93,7 @@ func (l *StdLogger) Fatal(message string, params ...interface{}) {
func (l *StdLogger) Panic(message string, params ...interface{}) { func (l *StdLogger) Panic(message string, params ...interface{}) {
l.Log.Panic(fmt.Printf("PANIC: %s - %s", fmt.Sprintf(message, params...), l.getKeys())) l.Log.Panic(fmt.Printf("PANIC: %s - %s", fmt.Sprintf(message, params...), l.getKeys()))
} }
func (l *StdLogger) New(name string) Logger { func (l *StdLogger) New(name string) logadapter.Logger {
//nl := &StdLogger{keys: l.keys} //nl := &StdLogger{keys: l.keys}
nl := &StdLogger{level: l.level} nl := &StdLogger{level: l.level}
nl.Log.SetPrefix(fmt.Sprintf("%s.%s", l.Log.Prefix(), name)) nl.Log.SetPrefix(fmt.Sprintf("%s.%s", l.Log.Prefix(), name))
@ -91,7 +101,7 @@ func (l *StdLogger) New(name string) Logger {
nl.Log.SetOutput(l.Log.Writer()) nl.Log.SetOutput(l.Log.Writer())
return nl return nl
} }
func (l *StdLogger) With(key string, value interface{}) Logger { func (l *StdLogger) With(key string, value interface{}) logadapter.Logger {
l.mx.Lock() l.mx.Lock()
defer l.mx.Unlock() defer l.mx.Unlock()
stdlog := &StdLogger{level: l.level, keys: utils.CopyableMap(l.keys).DeepCopy()} stdlog := &StdLogger{level: l.level, keys: utils.CopyableMap(l.keys).DeepCopy()}

View file

@ -1,24 +1,47 @@
package logadapter /*
MIT License
Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package stdlogger_test
import ( import (
"bytes" "bytes"
"os" "os"
"regexp" "regexp"
"testing" "testing"
"github.com/Fishwaldo/go-logadapter/loggers/std"
) )
func TestDefaultLogger(t *testing.T) { func TestDefaultLogger(t *testing.T) {
logger := DefaultLogger() logger := stdlogger.DefaultLogger()
logger.SetLevel(LOG_TRACE) logger.SetLevel(stdlogger.LOG_TRACE)
if logger.level != LOG_TRACE { if logger.GetLevel() != stdlogger.LOG_TRACE {
t.Errorf("Can't Set Logging Level") t.Errorf("Can't Set Logging Level")
} }
if logger.GetLevel() != LOG_TRACE {
t.Error("GetLevel Didn't return Correct Logging Level")
}
} }
func captureOutput(l *StdLogger, f func()) string { func captureOutput(l *stdlogger.StdLogger, f func()) string {
var buf bytes.Buffer var buf bytes.Buffer
l.Log.SetOutput(&buf) l.Log.SetOutput(&buf)
f() f()
@ -26,8 +49,8 @@ func captureOutput(l *StdLogger, f func()) string {
return buf.String() return buf.String()
} }
func TestLogTrace(t *testing.T) { func TestLogTrace(t *testing.T) {
logger := DefaultLogger() logger := stdlogger.DefaultLogger()
logger.SetLevel(LOG_TRACE) logger.SetLevel(stdlogger.LOG_TRACE)
output := captureOutput(logger, func() { output := captureOutput(logger, func() {
logger.Trace("Hello %s", "world") logger.Trace("Hello %s", "world")
}) })
@ -37,8 +60,8 @@ func TestLogTrace(t *testing.T) {
} }
} }
func TestLogDebug(t *testing.T) { func TestLogDebug(t *testing.T) {
logger := DefaultLogger() logger := stdlogger.DefaultLogger()
logger.SetLevel(LOG_TRACE) logger.SetLevel(stdlogger.LOG_TRACE)
output := captureOutput(logger, func() { output := captureOutput(logger, func() {
logger.Debug("Hello %s", "world") logger.Debug("Hello %s", "world")
}) })
@ -49,8 +72,8 @@ func TestLogDebug(t *testing.T) {
} }
func TestLogInfo(t *testing.T) { func TestLogInfo(t *testing.T) {
logger := DefaultLogger() logger := stdlogger.DefaultLogger()
logger.SetLevel(LOG_TRACE) logger.SetLevel(stdlogger.LOG_TRACE)
output := captureOutput(logger, func() { output := captureOutput(logger, func() {
logger.Info("Hello %s", "world") logger.Info("Hello %s", "world")
}) })
@ -61,8 +84,8 @@ func TestLogInfo(t *testing.T) {
} }
func TestLogWarn(t *testing.T) { func TestLogWarn(t *testing.T) {
logger := DefaultLogger() logger := stdlogger.DefaultLogger()
logger.SetLevel(LOG_TRACE) logger.SetLevel(stdlogger.LOG_TRACE)
output := captureOutput(logger, func() { output := captureOutput(logger, func() {
logger.Warn("Hello %s", "world") logger.Warn("Hello %s", "world")
}) })
@ -73,8 +96,8 @@ func TestLogWarn(t *testing.T) {
} }
func TestLogError(t *testing.T) { func TestLogError(t *testing.T) {
logger := DefaultLogger() logger := stdlogger.DefaultLogger()
logger.SetLevel(LOG_TRACE) logger.SetLevel(stdlogger.LOG_TRACE)
output := captureOutput(logger, func() { output := captureOutput(logger, func() {
logger.Error("Hello %s", "world") logger.Error("Hello %s", "world")
}) })
@ -97,8 +120,8 @@ func TestLogFatal(t *testing.T) {
} }
func TestLogPanic(t *testing.T) { func TestLogPanic(t *testing.T) {
logger := DefaultLogger() logger := stdlogger.DefaultLogger()
logger.SetLevel(LOG_TRACE) logger.SetLevel(stdlogger.LOG_TRACE)
defer func() { defer func() {
if err := recover(); err == nil { if err := recover(); err == nil {
t.Errorf("Log Panic Recovery Failed") t.Errorf("Log Panic Recovery Failed")

View file

@ -1,3 +1,27 @@
/*
MIT License
Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package zaplog package zaplog
import ( import (

138
loggers/zap/zap_test.go Normal file
View file

@ -0,0 +1,138 @@
/*
MIT License
Copyright (c) 2021 Justin Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package zaplog_test
import (
// "bytes"
// "os"
// "regexp"
"testing"
// "github.com/Fishwaldo/go-logadapter/loggers/zap"
)
func TestDefaultLogger(t *testing.T) {
//_ := zaplog.ZapLogger{}
//logger.SetLevel(stdlogger.LOG_TRACE)
//if logger.GetLevel() != stdlogger.LOG_TRACE {
// t.Errorf("Can't Set Logging Level")
//}
}
// func captureOutput(l *zaplog.ZapLogger, f func()) string {
// // var buf bytes.Buffer
// // l.Log.SetOutput(&buf)
// // f()
// // l.Log.SetOutput(os.Stderr)
// // return buf.String()
// }
func TestLogTrace(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// output := captureOutput(logger, func() {
// logger.Trace("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* TRACE: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Trace Failed: %s", output)
// }
}
func TestLogDebug(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// output := captureOutput(logger, func() {
// logger.Debug("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* DEBUG: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Debug Failed: %s", output)
// }
}
func TestLogInfo(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// output := captureOutput(logger, func() {
// logger.Info("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* INFO: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Info Failed: %s", output)
// }
}
func TestLogWarn(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// output := captureOutput(logger, func() {
// logger.Warn("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* WARN: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Warn Failed: %s", output)
// }
}
func TestLogError(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// output := captureOutput(logger, func() {
// logger.Error("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* ERROR: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Error Failed: %s", output)
// }
}
func TestLogFatal(t *testing.T) {
//logger := DefaultLogger()
//logger.SetLevel(LOG_TRACE)
//output := captureOutput(logger, func() {
// logger.Fatal("Hello %s", "world")
//})
//validmsg := regexp.MustCompile(`^.* FATAL: Hello world \- {}`)
//if !validmsg.MatchString(output) {
// t.Errorf("Log Fatal Failed: %s", output)
//}
}
func TestLogPanic(t *testing.T) {
// logger := stdlogger.DefaultLogger()
// logger.SetLevel(stdlogger.LOG_TRACE)
// defer func() {
// if err := recover(); err == nil {
// t.Errorf("Log Panic Recovery Failed")
// }
// }()
// output := captureOutput(logger, func() {
// logger.Panic("Hello %s", "world")
// })
// validmsg := regexp.MustCompile(`^.* PANIC: Hello world \- {}`)
// if !validmsg.MatchString(output) {
// t.Errorf("Log Panic Failed: %s", output)
// }
}

View file

@ -1,48 +0,0 @@
package utils
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestCopyMap(t *testing.T) {
m1 := map[string]interface{}{
"a": "bbb",
"b": map[string]interface{}{
"c": 123,
},
"c": []interface{} {
"d", "e", map[string]interface{} {
"f": "g",
},
},
}
m2 := CopyableMap(m1).DeepCopy()
m1["a"] = "zzz"
delete(m1, "b")
m1["c"].([]interface{})[1] = "x"
m1["c"].([]interface{})[2].(map[string]interface{})["f"] = "h"
require.Equal(t, map[string]interface{}{
"a": "zzz",
"c": []interface{} {
"d", "x", map[string]interface{} {
"f": "h",
},
},
}, m1)
require.Equal(t, map[string]interface{}{
"a": "bbb",
"b": map[string]interface{}{
"c": 123,
},
"c": []interface{} {
"d", "e", map[string]interface{} {
"f": "g",
},
},
}, m2)
}