log

command
v1.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

Log interceptor

In this example, we will try to create gin server with log interceptor enabled.

Table of Contents generated with DocToc

Quick start

Get rk-gin package from the remote repository.

go get -u github.com/rookie-ninja/rk-gin
Code
import     "github.com/rookie-ninja/rk-gin/interceptor/log/zap"
    interceptors := []gin.HandlerFunc{
        rkginlog.Interceptor(),
    }

Options

Log interceptor will init rkquery.Event, zap.Logger and entryName which will be injected into request context before user function. As soon as user function returns, interceptor will write the event into files.

arch

Name Default Description
WithEntryNameAndType(entryName, entryType string) entryName=grpc, entryType=grpc entryName and entryType will be used to distinguish options if there are multiple interceptors in single process.
WithZapLoggerEntry(zapLoggerEntry *rkentry.ZapLoggerEntry) rkentry.GlobalAppCtx.GetZapLoggerEntryDefault() Zap logger would print to stdout with console encoding type.
WithEventLoggerEntry(eventLoggerEntry *rkentry.EventLoggerEntry) rkentry.GlobalAppCtx.GetEventLoggerEntryDefault() Event logger would print to stdout with console encoding type.
WithZapLoggerEncoding(ec int) rkginlog.ENCODING_CONSOLE rkginlog.ENCODING_CONSOLE and rkginlog.ENCODING_JSON are available options.
WithZapLoggerOutputPaths(path ...string) stdout Both absolute path and relative path is acceptable. Current working directory would be used if path is relative.
WithEventLoggerEncoding(ec int) rkginlog.ENCODING_CONSOLE rkginlog.ENCODING_CONSOLE and rkginlog.ENCODING_JSON are available options.
WithEventLoggerOutputPaths(path ...string) stdout Both absolute path and relative path is acceptable. Current working directory would be used if path is relative.
    // ********************************************
    // ********** Enable interceptors *************
    // ********************************************
    interceptors := []gin.HandlerFunc{
        rkginlog.Interceptor(
            // Entry name and entry type will be used for distinguishing interceptors. Recommended.
            // rkginlog.WithEntryNameAndType("greeter", "grpc"),
            //
            // Zap logger would be logged as JSON format.
            // rkginlog.WithZapLoggerEncoding(rkgrpclog.ENCODING_JSON),
            //
            // Event logger would be logged as JSON format.
            // rkginlog.WithEventLoggerEncoding(rkgrpclog.ENCODING_JSON),
            //
            // Zap logger would be logged to specified path.
            // rkginlog.WithZapLoggerOutputPaths("logs/server-zap.log"),
            //
            // Event logger would be logged to specified path.
            // rkginlog.WithEventLoggerOutputPaths("logs/server-event.log"),
        ),
    }
Encoding
  • CONSOLE No options needs to be provided.
2021-06-24T21:01:24.633+0800    INFO    log/greeter-server.go:84        Received request from client.
------------------------------------------------------------------------
endTime=2021-06-24T21:01:24.634+08:00
startTime=2021-06-24T21:01:24.633836+08:00
elapsedNano=164521
timezone=CST
ids={"eventId":"5cdcb51d-4e92-4d9e-afce-381bea1bf6de"}
app={"appName":"rk","appVersion":"v0.0.0","entryName":"gin","entryType":"gin"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/rk/v1/greeter","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost:59410
operation=/rk/v1/greeter
resCode=200
eventStatus=Ended
EOE
  • JSON
    // ********************************************
    // ********** Enable interceptors *************
    // ********************************************
    interceptors := []gin.HandlerFunc{
        rkginlog.Interceptor(
            // Zap logger would be logged as JSON format.
            rkginlog.WithZapLoggerEncoding(rkginlog.ENCODING_JSON),
            //
            // Event logger would be logged as JSON format.
            rkginlog.WithEventLoggerEncoding(rkginlog.ENCODING_JSON),
        ),
    }
{"level":"INFO","ts":"2021-06-24T21:17:14.995+0800","msg":"Received request from client."}
{"endTime": "2021-06-24T21:17:14.995+0800", "startTime": "2021-06-24T21:17:14.995+0800", "elapsedNano": 148030, "timezone": "CST", "ids": {"eventId":"03e71ee0-428f-4830-85b6-5ce56108907e"}, "app": {"appName":"rk","appVersion":"v0.0.0","entryName":"gin","entryType":"gin"}, "env": {"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}, "payloads": {"apiMethod":"GET","apiPath":"/rk/v1/greeter","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}, "error": {}, "counters": {}, "pairs": {}, "timing": {}, "remoteAddr": "localhost:63320", "operation": "/rk/v1/greeter", "eventStatus": "Ended", "resCode": "200"}
OutputPath
  • Stdout No options needs to be provided.

  • Files

    // ********************************************
    // ********** Enable interceptors *************
    // ********************************************
    interceptors := []gin.HandlerFunc{
        rkginlog.Interceptor(
            // Event logger would be logged as JSON format.
            // rkginlog.WithEventLoggerEncoding(rkginlog.ENCODING_JSON),
            //
            // Zap logger would be logged to specified path.
            rkginlog.WithZapLoggerOutputPaths("logs/server-zap.log"),
            //
            // Event logger would be logged to specified path.
            rkginlog.WithEventLoggerOutputPaths("logs/server-event.log"),
        ),
    }
Context Usage
Name Functionality
rkginctx.GetLogger(*gin.Context) Get logger generated by log interceptor. If there are X-Request-Id or X-Trace-Id as headers in incoming and outgoing metadata, then loggers will has requestId and traceId attached by default.
rkginctx.GetEvent(*gin.Context) Get event generated by log interceptor. Event would be printed as soon as RPC finished.
rkginctx.GetIncomingHeaders(*gin.Context) Get incoming header.
rkginctx.AddHeaderToClient(ctx, "k", "v") Add k/v to headers which would be sent to client. This is append operation.
rkginctx.SetHeaderToClient(ctx, "k", "v") Set k/v to headers which would be sent to client.

Example

In this example, we enable log interceptor.

Start server
$ go run greeter-server.go
Output
  • Server side (zap & event)
2021-06-24T21:23:06.100+0800    INFO    log/greeter-server.go:84        Received request from client.
------------------------------------------------------------------------
endTime=2021-06-24T21:23:06.100956+08:00
startTime=2021-06-24T21:23:06.10083+08:00
elapsedNano=125493
timezone=CST
ids={"eventId":"b3dd6eb6-316a-4f58-b8b0-3429ef46a2ea"}
app={"appName":"rk","appVersion":"v0.0.0","entryName":"gin","entryType":"gin"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/rk/v1/greeter","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost:64769
operation=/rk/v1/greeter
resCode=200
eventStatus=Ended
EOE
  • Client side
$ curl "localhost:8080/rk/v1/greeter?name=rk-dev"
{"Message":"Hello rk-dev!"}
Code

Documentation

Overview

Copyright (c) 2021 rookie-ninja

Use of this source code is governed by an Apache-style license that can be found in the LICENSE file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL