This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support use logrus to transmit trace context to log (#19)
- Loading branch information
Showing
7 changed files
with
168 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Go2sky with logrus (v1.8.1) | ||
|
||
## Installation | ||
|
||
```bash | ||
go get -u github.com/SkyAPM/go2sky-plugins/logrus | ||
``` | ||
|
||
## Usage | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/sirupsen/logrus" | ||
logrusplugin "github.com/SkyAPM/go2sky-plugins/logrus" | ||
) | ||
|
||
func main() { | ||
// init format with custom trace context key | ||
// SW_CTX format: [$serviceName,$instanceName,$traceId,$traceSegmentId,$spanId] | ||
logrus.SetFormatter(logrusplugin.Wrap(&logrus.JSONFormatter{}, "SW_CTX"})) | ||
|
||
// init tracer | ||
|
||
// log with context | ||
ctx := context.Background() | ||
logrus.WithContext(ctx).Info("test1") | ||
} | ||
``` | ||
|
||
[See more](example_logrus_test.go). |
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,19 @@ | ||
// Licensed to SkyAPM org under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. SkyAPM org licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
// Package logrus is a plugin that can be transmit trace context to the log framework. | ||
package logrus |
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,54 @@ | ||
// Licensed to SkyAPM org under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. SkyAPM org licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package logrus | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/SkyAPM/go2sky" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func ExampleWrapFormat() { | ||
context := context.Background() | ||
|
||
logrus.SetOutput(os.Stdout) | ||
|
||
// init tracer | ||
_, err := go2sky.NewTracer("example") | ||
if err != nil { | ||
logrus.Fatalf("create tracer error %v \n", err) | ||
} | ||
|
||
// json format | ||
logrus.SetFormatter(Wrap(&logrus.JSONFormatter{ | ||
DisableTimestamp: true, | ||
}, "SW_CTX")) | ||
logrus.WithContext(context).Info("test1") | ||
|
||
// test format | ||
logrus.SetFormatter(Wrap(&logrus.TextFormatter{ | ||
DisableTimestamp: true, | ||
}, "SW_CTX")) | ||
logrus.WithContext(context).Info("test2") | ||
|
||
// Output: | ||
// {"SW_CTX":"[,,N/A,N/A,-1]","level":"info","msg":"test1"} | ||
// level=info msg=test2 SW_CTX="[,,N/A,N/A,-1]" | ||
} |
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 @@ | ||
module github.com/SkyAPM/go2sky-plugins/logrus | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/SkyAPM/go2sky v1.0.1-0.20210518045634-6c4fc8fa5f1e | ||
github.com/sirupsen/logrus v1.8.1 | ||
) |
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,48 @@ | ||
// Licensed to SkyAPM org under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. SkyAPM org licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package logrus | ||
|
||
import ( | ||
"github.com/SkyAPM/go2sky/log" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// WrapFormat is wrap format to transmit trace context when logging | ||
type WrapFormat struct { | ||
Base logrus.Formatter | ||
traceContextKey string | ||
} | ||
|
||
// Wrap original format | ||
func Wrap(base logrus.Formatter, contextKey string) *WrapFormat { | ||
if contextKey == "" { | ||
contextKey = "SW_CTX" | ||
} | ||
|
||
return &WrapFormat{base, contextKey} | ||
} | ||
|
||
// Format logging with trace context | ||
func (format *WrapFormat) Format(entry *logrus.Entry) ([]byte, error) { | ||
// append trace context | ||
if entry.Context != nil { | ||
entry.Data[format.traceContextKey] = log.FromContext(entry.Context).String() | ||
} | ||
|
||
return format.Base.Format(entry) | ||
} |