kataras / golog

A high-performant Logging Foundation for Go Applications. X3 faster than the rest leveled loggers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

shared or main logger

yoni386 opened this issue · comments

Hello,

Thanks for this library.

What is the best way to share logger between different packages for example main has package One, Two, Three what will be the best to share the same logger (same config) ?

golog.Default is the shared main logger, so you can use that, it's there already :)

Not in mu case.

For example: I have two packages or I set SetTimeFormat on main and use glog on different package the config is not global to both package. What will be the best way to set it once "globally" ?

This can't be done via the package alone atm, you mean two different applications? You need a way to communicate and link those apps in order to send and receive messages, if the apps can run in different machines you can do it via tcp. Or I didn't understand correctly?

Normally will use applications with support of data streaming for the logging but maybe this is not your case, I want to implement that, you gave me a new idea right now (I'm designing a new package the last weeks which can give us this feature to the kataras/golog package) but I want to know: what do you mean a shared or main logger?, write down the go source code you would love to write to do that and what you expect on your log output (i.e terminal) so I can start coding this feature.

Looks like golog.Default is the way.. I guess it is using sync.Once.
I mean one application is using two packages packageA and packageB and I want to set globally the logger config and use in some-cases Verbose logger - might be done with child?

I thought using golog.Child but this might be "abuse" to create two global loggers.

BTW, the order of golog.Child and golog.Handle is important.
if thought useing child to do custom layout

if golog.Handle is set first and then golog.Child.Handle is second the config of golog.Child.Handle will be ignored the opposite works.
if golog.Child("simple").Handle(func(l *golog.Log) bool {}.. is set first and then
golog.Handle(func(l *golog.Log) bool {...} # it will work

This is ok:

	golog.Child("simple").Handle(func(l *golog.Log) bool {
		message := fmt.Sprintf("%s", l.Message)

		if l.NewLine {
			message += "\n"
		}

		fmt.Print(message)
		return true
	})

	golog.Handle(func(l *golog.Log) bool {
		prefix := golog.GetTextForLevel(l.Level, false)
		_, fn, line, _ := runtime.Caller(6)
		fn = filepath.Base(fn)
		message := fmt.Sprintf("%s %s %s:%d %s", prefix, "03/01/2006 15:04", fn, line, l.Message)

		if l.NewLine {
			message += "\n"
		}

		fmt.Print(message)
		return true
	})

This is not ok:


	golog.Handle(func(l *golog.Log) bool {
		prefix := golog.GetTextForLevel(l.Level, false)
		_, fn, line, _ := runtime.Caller(6)
		fn = filepath.Base(fn)
		message := fmt.Sprintf("%s %s %s:%d %s", prefix, "03/01/2006 15:04", fn, line, l.Message)

		if l.NewLine {
			message += "\n"
		}

		fmt.Print(message)
		return true
	})


	golog.Child("simple").Handle(func(l *golog.Log) bool {
		message := fmt.Sprintf("%s", l.Message)

		if l.NewLine {
			message += "\n"
		}

		fmt.Print(message)
		return true
	})


Yes, if you mean two packages, it's the golog.Default as we've already noted, and yes you have to .Handle first and after make Child in order to inherite the custom log handler. You don't have problem, the internal kataras/pio#Printer does the sync needed so you should be fine, and if not you can use sync.Mutex in your .Handle (although you will not need those but just in case you ever need to do something crazy inside there, you can do it)