0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Tips】Golangでログ出力箇所のパスをログに追加する。

Last updated at Posted at 2019-05-27

お題

表題の通り。 Goではruntime.Callerを使うと、その関数の呼び出し箇所の情報が取れるので、それを使う。

実践

ソースは下記。
https://github.com/sky0621/tips-go/tree/master/try/log

共通ロガー

[logger/logger.go]
package logger

import (
	"fmt"
	"runtime"
)

type AppLogger interface {
	Log(msg string)
}

type appLogger struct {
}

func NewAppLogger() AppLogger {
	return &appLogger{}
}

func (l *appLogger) Log(msg string) {
	fmt.Printf("[Path:%s] %s\n", fileWithLineNum(), msg)
}

func fileWithLineNum() string {
	_, name, line, ok := runtime.Caller(2)
	if !ok {
		return "-"
	}
	return fmt.Sprintf("%s:%d", name, line)
}

ドメイン層

呼び出し箇所が階層化されているケースでちゃんとログにパスが吐かれることの確認用に用意。

[domain/user.go]
package domain

import "tips-go/try/log/logger"

type User interface {
	Hello()
}

type user struct {
	lgr logger.AppLogger
}

func NewUser(lgr logger.AppLogger) User {
	return &user{lgr: lgr}
}

func (u *user) Hello() {
	u.lgr.Log("Hello!")
}

メイン関数

[main.go]
package main

import (
	"tips-go/try/log/domain"
	"tips-go/try/log/logger"
)

func main() {
	lgr := logger.NewAppLogger()
	user := domain.NewUser(lgr)
	user.Hello()
}

実行結果

$ go run main.go 
[Path:〜ルートからのフルパス〜/tips-go/try/log/domain/user.go:18] Hello!
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?