Code Monkey home page Code Monkey logo

logger-http's Introduction

logger-http

Build Status Go Report Card GoDoc

Gol4ng logger sub package for logging http Related package gol4ng/logger and gol4ng/httpware

Installation

go get -u github.com/gol4ng/logger-http

Quick Start

You can refer at gol4ng/httpware documentation for the middleware/tripperware usage

Tripperware

Log you're http.Client request

<debug> http client gonna GET http://google.com {"http_url":"http://google.com","http_start_time":"2019-12-13T17:01:13+01:00","http_kind":"client","Correlation-Id":"yhyBI94zyl","http_header":{"Correlation-Id":["yhyBI94zyl"]},"http_method":"GET"}
<info> http client GET http://google.com [status_code:301, duration:39.70726ms, content_length:219] {"Correlation-Id":"yhyBI94zyl","http_start_time":"2019-12-13T17:01:13+01:00","http_method":"GET","http_duration":0.03970726,"http_response_length":219,"http_header":{"Correlation-Id":["yhyBI94zyl"]},"http_url":"http://google.com","http_status":"301 Moved Permanently","http_status_code":301,"http_kind":"client"}
<debug> http client gonna GET http://www.google.com/ {"http_kind":"client","Correlation-Id":"uQzaMO9JC0","http_header":{"Correlation-Id":["uQzaMO9JC0"],"Referer":["http://google.com"]},"http_method":"GET","http_url":"http://www.google.com/","http_start_time":"2019-12-13T17:01:13+01:00"}
<info> http client GET http://www.google.com/ [status_code:200, duration:72.582736ms, content_length:-1] {"Correlation-Id":"uQzaMO9JC0","http_header":{"Correlation-Id":["uQzaMO9JC0"],"Referer":["http://google.com"]},"http_method":"GET","http_kind":"client","http_response_length":-1,"http_duration":0.072582736,"http_status_code":200,"http_url":"http://www.google.com/","http_start_time":"2019-12-13T17:01:13+01:00","http_status":"200 OK"}
package main

import (
	"net/http"
	"os"

	"github.com/gol4ng/httpware/v4"
	"github.com/gol4ng/logger"
	"github.com/gol4ng/logger-http/tripperware"
	"github.com/gol4ng/logger/formatter"
	"github.com/gol4ng/logger/handler"
)

func main(){
	// logger will print on STDOUT with default line format
	myLogger := logger.NewLogger(handler.Stream(os.Stdout, formatter.NewDefaultFormatter()))

	clientStack := httpware.TripperwareStack(
		tripperware.InjectLogger(myLogger),
		tripperware.CorrelationId(),
		tripperware.Logger(myLogger),
	)

	c := http.Client{
		Transport: clientStack.DecorateRoundTripper(http.DefaultTransport),
	}

	c.Get("http://google.com")
	// Will log
	//<info> http client GET http://google.com [status_code:301, duration:27.524999ms, content_length:219] {"http_duration":0.027524999,"http_status":"301 Moved Permanently","http_status_code":301,"http_response_length":219,"http_method":"GET","http_url":"http://google.com","http_start_time":"2019-12-03T10:47:38+01:00","http_kind":"client"}
	//<info> http client GET http://www.google.com/ [status_code:200, duration:51.047002ms, content_length:-1] {"http_kind":"client","http_duration":0.051047002,"http_status":"200 OK","http_status_code":200,"http_response_length":-1,"http_method":"GET","http_url":"http://www.google.com/","http_start_time":"2019-12-03T10:47:38+01:00"}
}

Middleware

Log you're incoming http server request

<debug> http server received GET / {"http_url":"/","http_start_time":"2019-12-13T17:15:30+01:00","http_kind":"server","Correlation-Id":"SBeEdhRhUl","http_header":{"Accept-Encoding":["gzip"],"Correlation-Id":["SBeEdhRhUl"],"User-Agent":["Go-http-client/1.1"]},"http_method":"GET"}
<info> handler log info {"Correlation-Id":"SBeEdhRhUl"}
<info> http server GET / [status_code:200, duration:290.156µs, content_length:0] {"http_kind":"server","http_duration":0.000290156,"http_status_code":200,"Correlation-Id":"SBeEdhRhUl","http_method":"GET","http_url":"/","http_start_time":"2019-12-13T17:15:30+01:00","http_status":"OK","http_response_length":0,"http_header":{"Accept-Encoding":["gzip"],"Correlation-Id":["SBeEdhRhUl"],"User-Agent":["Go-http-client/1.1"]}}
package main
     
     import (
     	"context"
     	"net"
     	"net/http"
     	"os"
     
     	"github.com/gol4ng/httpware/v4"
     	"github.com/gol4ng/logger"
     	"github.com/gol4ng/logger-http/middleware"
     	"github.com/gol4ng/logger/formatter"
     	"github.com/gol4ng/logger/handler"
     )
     
     func main() {
     	addr := ":5001"
     
     	myLogger := logger.NewLogger(
     		handler.Stream(os.Stdout, formatter.NewDefaultFormatter()),
     	)
     
     	// we recommend to use MiddlewareStack to simplify managing all wanted middlewares
     	// caution middleware order matters
     	stack := httpware.MiddlewareStack(
     		//middleware.InjectLogger(myLogger), // we recommend to use http.Server.BaseContext instead of this middleware
     		middleware.CorrelationId(),
     		middleware.Logger(myLogger),
     	)
     
     	h := http.HandlerFunc(func(writer http.ResponseWriter, innerRequest *http.Request) {
     		l := logger.FromContext(innerRequest.Context(), myLogger)
     		l.Info("handler log info", nil)
     	})
     
     	server := http.Server{
     		Addr:    addr,
     		Handler: stack.DecorateHandler(h),
     		BaseContext: func(listener net.Listener) context.Context {
     			return logger.InjectInContext(context.Background(), myLogger)
     		},
     	}
     
     	go func() {
     		if err := server.ListenAndServe(); err != nil {
     			panic(err)
     		}
     	}()
     
     	http.Get("http://localhost" + addr)
     
     	//<debug> http server received GET / {"Correlation-Id":"zEDWO9gmZ6","http_header":{"Accept-Encoding":["gzip"],"Correlation-Id":["zEDWO9gmZ6"],"User-Agent":["Go-http-client/1.1"]},"http_method":"GET","http_url":"/","http_start_time":"2019-12-13T17:05:53+01:00","http_kind":"server"}
     	//<info> handler log info {"Correlation-Id":"zEDWO9gmZ6"}
     	//<info> http server GET / [status_code:200, duration:232.491µs, content_length:0] {"Correlation-Id":"zEDWO9gmZ6","http_header":{"Accept-Encoding":["gzip"],"Correlation-Id":["zEDWO9gmZ6"],"User-Agent":["Go-http-client/1.1"]},"http_method":"GET","http_url":"/","http_start_time":"2019-12-13T17:05:53+01:00","http_duration":0.000232491,"http_status":"OK","http_status_code":200,"http_kind":"server","http_response_length":0}
     }

logger-http's People

Contributors

instabledesign avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

ldechoux

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.