cloudlog

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2021 License: MIT Imports: 3 Imported by: 0

README

cloudlog

GitHub tag (latest SemVer) GoDoc

Comments

If you have any feature requests, please feel free to create an issue. We're looking forward to communicate with you!

Summary

cloudlog provides structured loggging in Go.

We recommend to use cloudlog with GCP, support

format.

you can use logging features immediately when you only need default log config.

It uses zap internal.

Installation

$ go get -u github.com/ishihaya/cloudlog

Usage

You can define methods that you only use in Log interface.

logging methods list

package log

import (
	"os"
	"sync"

	"github.com/ishihaya/cloudlog"
)

// You can define methods that you only use.
type Log interface {
	Debugf(template string, args ...interface{})
	Infof(template string, args ...interface{})
	Warnf(template string, args ...interface{})
	Errorf(template string, args ...interface{})
	Fatalf(template string, args ...interface{})
	Debugw(msg string, keysAndValues ...interface{})
	Infow(msg string, keysAndValues ...interface{})
	Warnw(msg string, keysAndValues ...interface{})
	Errorw(msg string, keysAndValues ...interface{})
	Fatalw(msg string, keysAndValues ...interface{})
}

type log struct {
	*cloudlog.Logger
}

var sharedInstance Log
var once sync.Once

// You should call this if you use logger.
func GetInstance() Log {
	once.Do(func() {
		sharedInstance = new()
	})
	return sharedInstance
}

func new() Log {
	var logger *cloudlog.Logger
	var err error
	// serviceName is displayed in Error Reporting.
	serviceName := "backend-api"
	switch os.Getenv("APP_ENV") {
	// List runnning environments on cloud, such as GCP.
	case "production", "staging":
		logger, err = cloudlog.NewCloudLogger(
			cloudlog.NeedErrorReporting(true),
			cloudlog.ServiceName(serviceName),
		)
	default:
		logger, err = cloudlog.NewLocalLogger()
	}
	if err != nil {
		panic(err)
	}
	return &log{logger}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCloudErrorReportingOption

func AddCloudErrorReportingOption(zapLogger *zap.Logger) *zap.Logger

AddCloudErrorReportingOption add zap option about Error Reporting. You can use Error Reporting in GCP when you use this function. see: https://cloud.google.com/error-reporting/docs/formatting-error-messages?hl=ja#json_representation

func GetCloudServiceContextOption

func GetCloudServiceContextOption(serviceName string) zap.Option

GetCloudServiceContextOption get zap.Option about serviceName. You have to call this function when you use Error Reporting. e.g. cloud-run-backend-api

func NewCloudZapConfig

func NewCloudZapConfig(level, encoding string) zap.Config

NewCloudZapConfig provides default zap config in cloud environment.

func NewLocalZapConfig

func NewLocalZapConfig(level, encoding string) zap.Config

NewLocalZapConfig provides default zap config in local environment.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger provides structured logging in Go / Zap.

func NewCloudLogger

func NewCloudLogger(options ...Option) (logger *Logger, err error)

NewCloudLogger is logger constructor in Cloud Environment. It has optional arguments about logger options written on options.go. It has been set Error Reporting and Cloud Logging in GCP by default.

func NewLocalLogger

func NewLocalLogger(options ...Option) (logger *Logger, err error)

NewLocalLogger is logger constructor in Local Environment. It has optional arguments about logger options written on options.go.

func (*Logger) Debug

func (l *Logger) Debug(msg string, field ...zap.Field)

Debug logs a message at DebugLevel.

func (*Logger) Debugf

func (l *Logger) Debugf(template string, args ...interface{})

Debugf uses fmt.Sprintf to log a templated message.

func (*Logger) Debugw

func (l *Logger) Debugw(msg string, keysAndValues ...interface{})

Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*Logger) Error

func (l *Logger) Error(msg string, field ...zap.Field)

Error logs a message at ErrorLevel. If you don't need these wrapper methods, you can override them by parent interface defined on your own. You shoud use this method when you want faster logging, but if you don't want to use zap.Field and depend them, you can use Warnf or Warnw.

func (*Logger) Errorf

func (l *Logger) Errorf(template string, args ...interface{})

Errorf uses fmt.Sprintf to log a templated message. e.g. Errorf("something went wrong: %+v", err) If you don't need these wrapper methods, you can override them by parent interface defined on your own.

func (*Logger) Errorw

func (l *Logger) Errorw(msg string, keysAndValues ...interface{})

Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With. e.g. WarnW("something went wrong", "key", "value", "sum", 10) If you don't need these wrapper methods, you can override them by parent interface defined on your own.

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, field ...zap.Field)

Fatal logs a message at FatalLevel.

func (*Logger) Fatalf

func (l *Logger) Fatalf(template string, args ...interface{})

Fatalf uses fmt.Sprintf to log a templated message.

func (*Logger) Fatalw

func (l *Logger) Fatalw(msg string, keysAndValues ...interface{})

Fatalw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*Logger) Info

func (l *Logger) Info(msg string, field ...zap.Field)

Info logs a message at InfoLevel.

func (*Logger) Infof

func (l *Logger) Infof(template string, args ...interface{})

Infof uses fmt.Sprintf to log a templated message.

func (*Logger) Infow

func (l *Logger) Infow(msg string, keysAndValues ...interface{})

Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*Logger) Warn

func (l *Logger) Warn(msg string, field ...zap.Field)

Warn logs a message at WarnLevel. If you don't need these wrapper methods, you can override them by parent interface defined on your own. You shoud use this method when you want faster logging, but if you don't want to use zap.Field and depend them, you can use Warnf or Warnw.

func (*Logger) Warnf

func (l *Logger) Warnf(template string, args ...interface{})

Warnf uses fmt.Sprintf to log a templated message. e.g. Warnf("something went wrong: %+v", err) If you don't need these wrapper methods, you can override them by parent interface defined on your own.

func (*Logger) Warnw

func (l *Logger) Warnw(msg string, keysAndValues ...interface{})

Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With. e.g. WarnW("something went wrong", "key", "value", "sum", 10) If you don't need these wrapper methods, you can override them by parent interface defined on your own.

type Option

type Option func(*loggerOptions)

Option gives functions changed loggerOptions member. It uses Functional Option Pattern.

func LogLevel

func LogLevel(logLevel string) Option

LogLevel can assign logLevel.

func NeedErrorReporting

func NeedErrorReporting(needErrorReporting bool) Option

NeedErrorReporting can assign needErrorReporting.

func ServiceName

func ServiceName(serviceName string) Option

ServiceName can assign serviceName.

Jump to

Keyboard shortcuts

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