This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from gofor-little/feat/BetterTests
- Added Dependabot configuration file. - Added CI GitHub Action. - Added stale GitHub Action for managing stale pull requests and issues. - Updated .gitignore. - Updated go.mod to go v1.15. - Improved CloudWatch logger tests. - Updated README.md.
- Loading branch information
Showing
9 changed files
with
191 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
version: 2 | ||
updates: | ||
- package-ecosystem: gomod | ||
directory: "/" | ||
schedule: | ||
interval: daily | ||
time: "17:00" | ||
open-pull-requests-limit: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-go@v2 | ||
with: | ||
go-version: ^1.15 | ||
- run: | | ||
go get -v -t -d ./... | ||
- run: | | ||
go test -v ./... | ||
env: | ||
AWS_REGION: ${{ secrets.AWS_REGION }} | ||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Close stale issues and pull requests | ||
|
||
on: | ||
schedule: | ||
- cron: 0 0 * * * | ||
|
||
jobs: | ||
stale: | ||
name: Update stale issues and pull requests | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/stale@v3 | ||
with: | ||
repo-token: ${{ secrets.GITHUB_TOKEN }} | ||
stale-issue-message: This issue is stale because it has been open ${{ secrets.OPEN_DAYS }} days with no activity. Remove stale label or comment or this will be closed in ${{ secrets.STALE_DAYS }} days. | ||
stale-pr-message: This pull request is stale because it has been open ${{ secrets.OPEN_DAYS }} days with no activity. Remove stale label or comment or this will be closed in ${{ secrets.STALE_DAYS }} days. | ||
close-issue-message: This issue has been closed because it has been open for ${{ secrets.TOTAL_DAYS }} days with no activity. | ||
close-pr-message: This pull request has been closed because it has been open for ${{ secrets.TOTAL_DAYS }} days with no activity. | ||
stale-issue-label: stale-closing-soon | ||
stale-pr-label: stale-closing-soon | ||
close-issue-label: stale-closed | ||
close-pr-label: stale-closed | ||
days-before-stale: ${{ secrets.OPEN_DAYS }} | ||
days-before-close: ${{ secrets.STALE_DAYS }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,70 @@ | ||
# little logger | ||
## A package for structured logging | ||
|
||
![GitHub tag (latest SemVer pre-release)](https://img.shields.io/github/v/tag/gofor-little/log?include_prereleases) | ||
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/gofor-little/log) | ||
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://raw.githubusercontent.com/gofor-little/log/main/LICENSE) | ||
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/gofor-little/log/CI) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/gofor-little/log)](https://goreportcard.com/report/github.com/gofor-little/log) | ||
[![PkgGoDev](https://pkg.go.dev/badge/github.com/gofor-little/log)](https://pkg.go.dev/github.com/gofor-little/log) | ||
|
||
### Introduction | ||
* Structured logging | ||
* Supports AWS CloudWatch | ||
* Simple interface for implementing your own | ||
|
||
### Example | ||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/gofor-little/log" | ||
) | ||
|
||
func main() { | ||
// Standard logger writes to a user defined io.Writer. | ||
log.Log = log.NewStandardLogger(os.Stdout, log.Fields{ | ||
"tag": "standard_logger", | ||
}) | ||
|
||
// CloudWatch logger writes to an AWS CloudWatch log group. | ||
sess, err = session.NewSession() | ||
log.Log, err = log.NewCloudWatchLogger(sess, "CloudWatchLoggerTest", log.Fields{ | ||
"tag": "cloudWatchLoggerTest", | ||
}) | ||
if err != nil { | ||
t.Fatalf("failed to create new CloudWatchLogger: %v", err) | ||
} | ||
|
||
// Log at info, error and debug levels. | ||
log.Info(log.Fields{ | ||
"message": "info message", | ||
"bool": true, | ||
"int": 64, | ||
"float": 3.14159, | ||
}) | ||
|
||
log.Error(log.Fields{ | ||
"string": "error message", | ||
"bool": true, | ||
"int": 64, | ||
"float": 3.14159, | ||
}) | ||
|
||
log.Debug(log.Fields{ | ||
"string": "debug message", | ||
"bool": true, | ||
"int": 64, | ||
"float": 3.14159, | ||
}) | ||
} | ||
``` | ||
|
||
### Testing | ||
Ensure the following environment variables are set, usually with a .env file. | ||
* ```AWS_PROFILE``` (an AWS CLI profile name) | ||
* ```AWS_REGION``` (a valid AWS region) | ||
|
||
Run ```go test ./...``` in the root directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,71 @@ | ||
package log_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/gofor-little/env" | ||
|
||
"github.com/gofor-little/log" | ||
) | ||
|
||
func TestCloudWatchLogger(t *testing.T) { | ||
sess := session.Must(session.NewSessionWithOptions(session.Options{ | ||
Config: aws.Config{ | ||
Region: aws.String("ap-southeast-2"), | ||
}, | ||
Profile: "<PROFILE_NAME>", | ||
})) | ||
|
||
var sess *session.Session | ||
var err error | ||
|
||
if err := env.Load(".env"); err != nil { | ||
t.Log(".env file not found, ignore this if running in CI/CD Pipeline") | ||
} | ||
|
||
if env.Get("ENVIRONMENT", "ci/cd") == "development" { | ||
sess, err = session.NewSessionWithOptions(session.Options{ | ||
Config: aws.Config{ | ||
Region: aws.String(env.Get("AWS_REGION", "ap-southeast-2")), | ||
}, | ||
Profile: env.Get("AWS_PROFILE", "default"), | ||
}) | ||
} else { | ||
sess, err = session.NewSession() | ||
} | ||
if err != nil { | ||
t.Fatalf("failed to create new session.Session") | ||
} | ||
|
||
log.Log, err = log.NewCloudWatchLogger(sess, "CloudWatchLoggerTest", log.Fields{ | ||
"tag": "cloudWatchLoggerTest", | ||
}) | ||
if err != nil { | ||
t.Fatalf("failed to create new CloudWatchLogger: %v", err) | ||
} | ||
|
||
for i := 0; i < 100; i++ { | ||
if err := log.Info(log.Fields{ | ||
"message": fmt.Sprintf("test info message number: %v", i), | ||
}); err != nil { | ||
t.Fatalf("failed to write info log: %v", err) | ||
} | ||
if err := log.Info(log.Fields{ | ||
"string": "test info string", | ||
"bool": true, | ||
"int": 64, | ||
"float": 3.14159, | ||
}); err != nil { | ||
t.Fatalf("failed to write Info log message") | ||
} | ||
|
||
if err := log.Error(log.Fields{ | ||
"string": "test error string", | ||
"bool": true, | ||
"int": 64, | ||
"float": 3.14159, | ||
}); err != nil { | ||
t.Fatalf("failed to write Error log message") | ||
} | ||
|
||
if err := log.Debug(log.Fields{ | ||
"string": "test debug string", | ||
"bool": true, | ||
"int": 64, | ||
"float": 3.14159, | ||
}); err != nil { | ||
t.Fatalf("failed to write Debug log message") | ||
} | ||
|
||
time.Sleep(time.Second * 5) | ||
time.Sleep(time.Second) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
module github.com/gofor-little/log | ||
|
||
go 1.13 | ||
go 1.15 | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go v1.36.17 | ||
github.com/gofor-little/env v0.4.3 | ||
github.com/gofor-little/ts v0.1.0 | ||
github.com/stretchr/testify v1.5.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters