123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package log
- import (
- "encoding/json"
- "io"
- "log"
- "github.com/k0kubun/pp/v3"
- "github.com/pkg/errors"
- )
- func Error(f string, err error, params ...interface{}) error {
- err = errors.Wrap(err, f)
- log.Default().Println(err)
- for _, param := range params {
- switch param.(type) {
- case string:
- log.Default().Println(param)
- default:
- b, e := json.MarshalIndent(param, "", " ")
- if e != nil {
- continue
- }
- log.Default().Println(string(b))
- }
- }
- return err
- }
- func Errorf(e error, f string, a ...interface{}) {
- e = errors.Wrapf(e, f, a...)
- if e != nil {
- log.Default().Println(e.Error())
- }
- }
- func ErrorJ(e error, a ...interface{}) {
- if e == nil {
- return
- } else {
- log.Default().Println(e.Error())
- for _, v := range a {
- switch v.(type) {
- case string:
- log.Default().Println(v)
- default:
- pp.Print(v)
- }
- }
- }
- }
- func Info(p ...interface{}) {
- // log.Default().Print(msg)
- var b []byte
- var e error
- for _, v := range p {
- b, e = json.MarshalIndent(v, "", " ")
- if e != nil {
- Error("log Infof:", e, v)
- return
- }
- log.Default().Printf("%s\n", string(b))
- }
- }
- func Printf(format string, v ...interface{}) {
- log.Default().Printf(format, v...)
- }
- func Print(v ...interface{}) {
- log.Default().Println(v...)
- }
- func Fatal(method string, err error, params ...interface{}) error {
- err = errors.Wrap(err, method+" error: ")
- log.Default().Fatalln(err)
- for _, param := range params {
- b, e := json.MarshalIndent(param, "", " ")
- if e != nil {
- continue
- }
- log.Default().Println(string(b))
- }
- return err
- }
- func PrettyStruct(p interface{}, r interface{}) {
- b := p.(string)
- e := json.Unmarshal([]byte(b), r)
- if e != nil {
- Error("log PrettyStruct:", e, p)
- }
- pp.Print(r)
- }
- func InfoS(p ...interface{}) {
- for _, v := range p {
- pp.Print(v)
- }
- }
- func New(out io.Writer, prefix string) *log.Logger {
- flag := log.LstdFlags
- return log.New(out, prefix, flag)
- }
- func Path(path string) {
- log.Default().Println(path)
- }
- func Wrapf(err error, m string, a ...interface{}) error {
- return errors.Wrapf(err, m, a...)
- }
|