Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shared or main logger #6

Open
yoni386 opened this issue Mar 8, 2018 · 6 comments
Open

shared or main logger #6

yoni386 opened this issue Mar 8, 2018 · 6 comments

Comments

@yoni386
Copy link

yoni386 commented Mar 8, 2018

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) ?

@kataras
Copy link
Owner

kataras commented Mar 10, 2018

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

@kataras kataras closed this as completed Mar 10, 2018
@yoni386
Copy link
Author

yoni386 commented Mar 11, 2018

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" ?

@kataras
Copy link
Owner

kataras commented Mar 11, 2018

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?

@kataras
Copy link
Owner

kataras commented Mar 11, 2018

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.

@kataras kataras reopened this Mar 11, 2018
@yoni386
Copy link
Author

yoni386 commented Mar 11, 2018

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
	})


@kataras
Copy link
Owner

kataras commented Mar 14, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants