Documentation ¶
Overview ¶
MIT License
Copyright (c) 2021 hirosassa ¶
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Source:
https://github.com/hirosassa/zerodriver/blob/32567406b83903dc813682fd5f1999ebbf462f2d/http.go
Index ¶
- Constants
- func MakeHTTPAttr(req *http.Request, res *http.Response) slog.Attr
- func MakeHTTPAttrFromHTTPPayload(p HTTPPayload) slog.Attr
- func New(w io.Writer, opts HandlerOptions) *slog.Logger
- func NewHandler(w io.Writer, opts HandlerOptions) slog.Handler
- type GAELatency
- type HTTPPayload
- type HandlerOptions
- type Latency
- type LogEntrySourceLocation
Examples ¶
Constants ¶
const ( LevelDefault slog.Level = slog.LevelDebug - 2 LevelDebug slog.Level = slog.LevelDebug LevelInfo slog.Level = slog.LevelInfo LevelNotice slog.Level = slog.LevelWarn - 2 LevelWarning slog.Level = slog.LevelWarn LevelError slog.Level = slog.LevelError LevelCritical slog.Level = slog.LevelError + 2 LevelAlert slog.Level = slog.LevelError + 4 LevelEmergency slog.Level = slog.LevelError + 6 )
const ( MessageKey = "message" SeverityKey = "severity" HTTPKey = "httpRequest" SourceLocationKey = "logging.googleapis.com/sourceLocation" LabelKey = "logging.googleapis.com/labels" TraceKey = "logging.googleapis.com/trace" SpanIDKey = "logging.googleapis.com/spanId" TraceSampledKey = "logging.googleapis.com/trace_sampled" )
Variables ¶
This section is empty.
Functions ¶
func MakeHTTPAttr ¶
MakeHTTPAttr returns slog.Attr struct.
func MakeHTTPAttrFromHTTPPayload ¶
func MakeHTTPAttrFromHTTPPayload(p HTTPPayload) slog.Attr
func New ¶
func New(w io.Writer, opts HandlerOptions) *slog.Logger
Example ¶
package main import ( "log/slog" "os" "github.com/kitagry/slogdriver" ) func main() { logger := slogdriver.New(os.Stdout, slogdriver.HandlerOptions{AddSource: true}) logger = logger.With(slog.Group(slogdriver.LabelKey, slog.String("commonLabel", "hoge"))) logger.Info("Hello World", slog.Group(slogdriver.LabelKey, slog.String("specifiedLabel", "fuga"))) }
Output:
func NewHandler ¶
func NewHandler(w io.Writer, opts HandlerOptions) slog.Handler
Types ¶
type GAELatency ¶
GAELatency is the Latency for GAE and Cloud Run.
type HTTPPayload ¶
type HTTPPayload struct { RequestMethod string `json:"requestMethod"` RequestURL string `json:"requestUrl"` RequestSize string `json:"requestSize"` Status int `json:"status"` ResponseSize string `json:"responseSize"` UserAgent string `json:"userAgent"` RemoteIP string `json:"remoteIp"` ServerIP string `json:"serverIp"` Referer string `json:"referer"` Latency Latency `json:"latency"` CacheLookup bool `json:"cacheLookup"` CacheHit bool `json:"cacheHit"` CacheValidatedWithOriginServer bool `json:"cacheValidatedWithOriginServer"` CacheFillBytes string `json:"cacheFillBytes"` Protocol string `json:"protocol"` }
HTTPPayload is the struct consists of http request related components. Details are in following link. https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#HttpRequest
func MakeHTTPPayload ¶
func MakeHTTPPayload(req *http.Request, res *http.Response) HTTPPayload
MakeHTTPPayload returns a HTTPPayload struct.
type HandlerOptions ¶
type HandlerOptions struct { // ProjectId is Google Cloud Project ID // If you want to use trace_id, you should set this or set GOOGLE_CLOUD_PROJECT environment. // Cloud Shell and App Engine set this environment variable to the project ID, so use it if present. ProjectID string // When AddSource is true, the handler adds a ("logging.googleapis.com/sourceLocation", {"file":"path/to/file.go","line":"12"}) // attribute to the output indicating the source code position of the log statement. AddSource is false by default // to skip the cost of computing this information. AddSource bool // Level reports the minimum record level that will be logged. // The handler discards records with lower levels. // If Level is nil, the handler assumes LevelInfo. // The handler calls Level.Level for each record processed; // to adjust the minimum level dynamically, use a LevelVar. Level slog.Leveler }
type Latency ¶
type Latency any
Latency is the interface of the request processing latency on the server. The format of the Latency should differ for GKE and for GAE, Cloud Run.