Skip to content

Commit

Permalink
Rename zerolog2cloudwatch -> cloudwatchwriter (#5)
Browse files Browse the repository at this point in the history
* zerolog2cloudwatch -> cloudwatchwriter

* Update changelog

* make tests more reliable

* Replace NewWriter with New
  • Loading branch information
mec07 authored Aug 14, 2020
1 parent 7f49945 commit 9cb2ae2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 40 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.0] - 2020-08-14

### Changed

- Renamed the repo cloudwatchwriter (was zerolog2cloudwatch)

## [0.1.1] - 2020-08-14

### Changed
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# zerolog2cloudwatch
# cloudwatchwriter
Package to enable sending logs from zerolog to AWS CloudWatch.

## Usage
Expand All @@ -24,7 +24,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/mec07/zerolog2cloudwatch"
"github.com/mec07/cloudwatchwriter"
"github.com/rs/zerolog/log"
)
Expand All @@ -46,9 +46,9 @@ func setupZerolog(accessKeyID, secretKey string) (func(), error) {
return nil, fmt.Errorf("session.NewSession: %w", err)
}
cloudWatchWriter, err := zerolog2cloudwatch.NewWriter(sess, logGroupName, logStreamName)
cloudWatchWriter, err := cloudwatchwriter.New(sess, logGroupName, logStreamName)
if err != nil {
return nil, fmt.Errorf("zerolog2cloudwatch.NewWriter: %w", err)
return nil, fmt.Errorf("cloudwatchwriter.New: %w", err)
}
log.Logger = log.Output(cloudWatchWriter)
Expand All @@ -69,9 +69,9 @@ See the example directory for a working example.
### Write to CloudWatch and the console
What I personally prefer is to write to both CloudWatch and the console, e.g.
```
cloudWatchWriter, err := zerolog2cloudwatch.NewWriter(sess, logGroupName, logStreamName)
cloudWatchWriter, err := cloudwatchwriter.New(sess, logGroupName, logStreamName)
if err != nil {
return fmt.Errorf("zerolog2cloudwatch.NewWriter: %w", err)
return fmt.Errorf("cloudwatchwriter.New: %w", err)
}
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout}
log.Logger = log.Output(zerolog.MultiLevelWriter(consoleWriter, cloudWatchWriter))
Expand All @@ -80,17 +80,17 @@ log.Logger = log.Output(zerolog.MultiLevelWriter(consoleWriter, cloudWatchWriter
### Create a new zerolog Logger
Of course, you can create a new `zerolog.Logger` using this too:
```
cloudWatchWriter, err := zerolog2cloudwatch.NewWriter(sess, logGroupName, logStreamName)
cloudWatchWriter, err := cloudwatchwriter.New(sess, logGroupName, logStreamName)
if err != nil {
return fmt.Errorf("zerolog2cloudwatch.NewWriter: %w", err)
return fmt.Errorf("cloudwatchwriter.New: %w", err)
}
logger := zerolog.New(cloudWatchWriter).With().Timestamp().Logger()
```
and of course you can create a new `zerolog.Logger` which can write to both CloudWatch and the console:
```
cloudWatchWriter, err := zerolog2cloudwatch.NewWriter(sess, logGroupName, logStreamName)
cloudWatchWriter, err := cloudwatchwriter.New(sess, logGroupName, logStreamName)
if err != nil {
return fmt.Errorf("zerolog2cloudwatch.NewWriter: %w", err)
return fmt.Errorf("cloudwatchwriter.New: %w", err)
}
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout}
logger := zerolog.New(zerolog.MultiLevelWriter(consoleWriter, cloudWatchWriter)).With().Timestamp().Logger()
Expand Down
13 changes: 6 additions & 7 deletions cloudwatch_writer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zerolog2cloudwatch
package cloudwatchwriter

import (
"sync"
Expand Down Expand Up @@ -54,14 +54,13 @@ type CloudWatchWriter struct {
done chan struct{}
}

// NewWriter returns a pointer to a CloudWatchWriter struct, or an error.
func NewWriter(sess *session.Session, logGroupName, logStreamName string) (*CloudWatchWriter, error) {
return NewWriterWithClient(cloudwatchlogs.New(sess), defaultBatchInterval, logGroupName, logStreamName)
// New returns a pointer to a CloudWatchWriter struct, or an error.
func New(sess *session.Session, logGroupName, logStreamName string) (*CloudWatchWriter, error) {
return NewWithClient(cloudwatchlogs.New(sess), defaultBatchInterval, logGroupName, logStreamName)
}

// NewWriterWithClient returns a pointer to a CloudWatchWriter struct, or an
// error.
func NewWriterWithClient(client CloudWatchLogsClient, batchInterval time.Duration, logGroupName, logStreamName string) (*CloudWatchWriter, error) {
// NewWithClient returns a pointer to a CloudWatchWriter struct, or an error.
func NewWithClient(client CloudWatchLogsClient, batchInterval time.Duration, logGroupName, logStreamName string) (*CloudWatchWriter, error) {
writer := &CloudWatchWriter{
client: client,
queue: lane.NewQueue(),
Expand Down
34 changes: 17 additions & 17 deletions cloudwatch_writer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zerolog2cloudwatch_test
package cloudwatchwriter_test

import (
"encoding/json"
Expand All @@ -11,7 +11,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/mec07/zerolog2cloudwatch"
"github.com/mec07/cloudwatchwriter"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -180,9 +180,9 @@ func assertEqualLogMessages(t *testing.T, expectedLogs []*cloudwatchlogs.InputLo
func TestCloudWatchWriter(t *testing.T) {
client := &mockClient{}

cloudWatchWriter, err := zerolog2cloudwatch.NewWriterWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWriterWithClient: %v", err)
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()

Expand Down Expand Up @@ -237,9 +237,9 @@ func TestCloudWatchWriter(t *testing.T) {
func TestCloudWatchWriterBatchInterval(t *testing.T) {
client := &mockClient{}

cloudWatchWriter, err := zerolog2cloudwatch.NewWriterWithClient(client, 5*time.Second, "logGroup", "logStream")
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 5*time.Second, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWriterWithClient: %v", err)
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()

Expand Down Expand Up @@ -284,9 +284,9 @@ func TestCloudWatchWriterBatchInterval(t *testing.T) {
func TestCloudWatchWriterHit1MBLimit(t *testing.T) {
client := &mockClient{}

cloudWatchWriter, err := zerolog2cloudwatch.NewWriterWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWriterWithClient: %v", err)
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()

Expand All @@ -310,7 +310,7 @@ func TestCloudWatchWriterHit1MBLimit(t *testing.T) {
// so much data
assert.True(t, client.numLogs() > 0)

if err = client.waitForLogs(numLogs, 200*time.Millisecond); err != nil {
if err = client.waitForLogs(numLogs, 400*time.Millisecond); err != nil {
t.Fatal(err)
}

Expand All @@ -326,9 +326,9 @@ func TestCloudWatchWriterHit1MBLimit(t *testing.T) {
func TestCloudWatchWriterHit10kLimit(t *testing.T) {
client := &mockClient{}

cloudWatchWriter, err := zerolog2cloudwatch.NewWriterWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWriterWithClient: %v", err)
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()

Expand Down Expand Up @@ -367,9 +367,9 @@ func TestCloudWatchWriterHit10kLimit(t *testing.T) {
func TestCloudWatchWriterParallel(t *testing.T) {
client := &mockClient{}

cloudWatchWriter, err := zerolog2cloudwatch.NewWriterWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWriterWithClient: %v", err)
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()

Expand Down Expand Up @@ -404,9 +404,9 @@ func TestCloudWatchWriterParallel(t *testing.T) {
func TestCloudWatchWriterClose(t *testing.T) {
client := &mockClient{}

cloudWatchWriter, err := zerolog2cloudwatch.NewWriterWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWriterWithClient: %v", err)
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()

Expand Down Expand Up @@ -436,9 +436,9 @@ func TestCloudWatchWriterReportError(t *testing.T) {
putLogEventsShouldError: true,
}

cloudWatchWriter, err := zerolog2cloudwatch.NewWriterWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWriterWithClient: %v", err)
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()

Expand Down
10 changes: 5 additions & 5 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/mec07/zerolog2cloudwatch"
"github.com/mec07/cloudwatchwriter"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

const (
region = "eu-west-2"
logGroupName = "zerolog2cloudwatch"
logGroupName = "cloudwatchwriter"
logStreamName = "this-computer"
)

Expand All @@ -30,7 +30,7 @@ func main() {
defer close()

for i := 0; i < 10000; i++ {
logger.Info().Str("name", "zerolog2cloudwatch").Msg(fmt.Sprintf("Log %d", i))
logger.Info().Str("package", "cloudwatchwriter").Msg(fmt.Sprintf("Log %d", i))
}
}

Expand All @@ -43,9 +43,9 @@ func newCloudWatchLogger(accessKeyID, secretKey string) (zerolog.Logger, func(),
return log.Logger, nil, fmt.Errorf("session.NewSession: %w", err)
}

cloudWatchWriter, err := zerolog2cloudwatch.NewWriter(sess, logGroupName, logStreamName)
cloudWatchWriter, err := cloudwatchwriter.New(sess, logGroupName, logStreamName)
if err != nil {
return log.Logger, nil, fmt.Errorf("zerolog2cloudwatch.NewWriter: %w", err)
return log.Logger, nil, fmt.Errorf("cloudwatchwriter.New: %w", err)
}

consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/mec07/zerolog2cloudwatch
module github.com/mec07/cloudwatchwriter

go 1.14

Expand Down

0 comments on commit 9cb2ae2

Please sign in to comment.