Logging globally (across packages)

IamNaN

I have spent a lot of time now searching and reading posts on the subject but have note yet managed to fully get my question answered - or perhaps I just need more clarification on existing answers.

I have read this post which has the same title as mine, but it's about logging across go routines rather than packages.

What I'm trying to solve is logging across the main app and any packages it uses. I need a logger that can log to multiple locations (which can be done with io.MultiWriter) and can do things like log.Error() and log.Debug()

I know there are packages that do this and I know how to implement those things myself.

What I can't get my head around is how to properly use it with my packages.

One way is of course to create the logger in main and then pass it around to all functions that need logging. But it seems awkward.

My ideal solution would be to have a logger like the built in global logger from the log package, but with the added functionality as above.

I mostly want this for optional debug logging within packages, so I can turn this on in a production version if needed.

What is the proper way to do this?

Cedmundo

The proper way is what you think is the ideal way. Just create a package (preferably following Go's conventions https://golang.org/doc/code.html) and make your Log global:

package mylog

// Define your custom logger type.
type logger struct { /* Whatever you want */ }

// Optionally make it a interface.
type Logger interface { /* Your functions */ }

// And just go global.
var defaultLogger *Logger

func init(){
   defaultLogger = new(logger)
}

func Debug(params ...string){
   // Have some fun.
}

// ...

Also I would recommend to describe in documentation that your project uses that logging feature.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Installing Meteor packages globally

From Dev

Logging Identifiers across methods

From Dev

Running npm globally installed packages

From Dev

Check for tkinter events globally (across OS)

From Java

How to update globally installed npm packages

From Dev

Sharing a globally defined db conn with multiple packages

From Dev

Accessing variables across packages in Go

From Dev

Sharing structs across multiple packages

From Dev

Usage of go/parser across packages

From Dev

Accessing a logger across multiple modules in Python logging

From Dev

Accessing a logger across multiple modules in Python logging

From Dev

Python logging into several files across several modules

From Dev

How to configure a `dconf` key globally across all user accounts

From Dev

How to configure a `dconf` key globally across all user accounts

From Dev

How to enable crash reports/core dumps/stack trace logging globally?

From Dev

Autowired Spring Beans across Java packages

From Dev

Create constants visible across packages, accessible directly

From Dev

HQL Inner Join across different packages

From Dev

An alternative to package.json, for npm (for globally installed packages)

From Dev

Composer - Is it possible to install packages in "require-dev" globally with one command?

From Dev

Globally instaling packages doesn't work (node.js, npm)

From Dev

An alternative to package.json, for npm (for globally installed packages)

From Dev

Composer - Is it possible to install packages in "require-dev" globally with one command?

From Dev

Python logging config file modules and packages

From Dev

How to make logging components visible across whole Spring Boot application

From Dev

Set global minimum logging level across all loggers in Python/Django

From Java

Find and version bump outdated packages in Flutter (across major versions)

From Java

JDK 15 Sealed Classes - how to use across packages?

From Dev

Is it possible to keep my Nix packages in sync across machines not running NixOS?

Related Related

  1. 1

    Installing Meteor packages globally

  2. 2

    Logging Identifiers across methods

  3. 3

    Running npm globally installed packages

  4. 4

    Check for tkinter events globally (across OS)

  5. 5

    How to update globally installed npm packages

  6. 6

    Sharing a globally defined db conn with multiple packages

  7. 7

    Accessing variables across packages in Go

  8. 8

    Sharing structs across multiple packages

  9. 9

    Usage of go/parser across packages

  10. 10

    Accessing a logger across multiple modules in Python logging

  11. 11

    Accessing a logger across multiple modules in Python logging

  12. 12

    Python logging into several files across several modules

  13. 13

    How to configure a `dconf` key globally across all user accounts

  14. 14

    How to configure a `dconf` key globally across all user accounts

  15. 15

    How to enable crash reports/core dumps/stack trace logging globally?

  16. 16

    Autowired Spring Beans across Java packages

  17. 17

    Create constants visible across packages, accessible directly

  18. 18

    HQL Inner Join across different packages

  19. 19

    An alternative to package.json, for npm (for globally installed packages)

  20. 20

    Composer - Is it possible to install packages in "require-dev" globally with one command?

  21. 21

    Globally instaling packages doesn't work (node.js, npm)

  22. 22

    An alternative to package.json, for npm (for globally installed packages)

  23. 23

    Composer - Is it possible to install packages in "require-dev" globally with one command?

  24. 24

    Python logging config file modules and packages

  25. 25

    How to make logging components visible across whole Spring Boot application

  26. 26

    Set global minimum logging level across all loggers in Python/Django

  27. 27

    Find and version bump outdated packages in Flutter (across major versions)

  28. 28

    JDK 15 Sealed Classes - how to use across packages?

  29. 29

    Is it possible to keep my Nix packages in sync across machines not running NixOS?

HotTag

Archive