Skip to content

canghel3/telemetry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Telemetry package

A simple logging package.

go get github.com/canghel3/telemetry@v1.1.2

Output

Built-in outputs:

  1. File logging
//Logging in-line
log.File(filename).Info().Log([]byte("hello, world!"))

//Reusing the same output destination
toFile := log.File(filename)

toFile.Info().Log("foo")
toFile.Error().Logf("encountered error: %s", "sample error")
  1. Stdout logging
//Logging in-line
log.Stdout().Info().Log("hello, world!")

//Reusing the same output destination
stdout := log.Stdout()

stdout.Info().Log("foo")
stdout.Error().Logf("encountered error: %s", "sample error")

A log can also contain metadata.

//NOTE: re-using an output driver with metadata will result in all messages generated with this driver to contain the given metadata
log.Stdout().WithMetadata(map[any]any{"something":"clean"})

Extendable
Supports addition of custom output drivers for logging to any custom implementation.

type example struct {
	msg []byte
}

func (e example) Write(b []byte) (int, error) {
	e.msg = b
	return len(b), nil
}


ex := example{}
log.OutputDriver(ex).Warn().Log("warning")

Levels

Built-in levels:

  1. Info
  2. Error
  3. Warn
  4. Debug
log.Stdout().Info().Log("hello world")

Extendable
Supports addition of self defined levels.

//In-line
log.Stdout().Level(level.Custom("MAJOR")).Log("major level")

//Reusing the level instance
criticalLevel := level.Custom("CRITICAL")
log.Stdout().Level(criticalLevel).Log("critical level")

Configuration

The log outputs can be customized using a configuration file. Configuration is limited to timestamp formatting and enabling/disabling implicit output formatting.

{
  "formatting": {
    "log" : {
      "disabled": false,
      "field_order":  {
        "timestamp": 1,
        "level": 2,
        "metadata": 3,
        "buffer": 4
      },
      "timestamp": "2024-03-02"
    },
    "transaction" : {
      "disabled": false,
      "field_order":  {
        "timestamp": 1,
        "level": 2,
        "metadata": 3,
        "buffer": 4
      },
      "timestamp": "2024-03-02"
    }
  }
}

Each logger instance can be modified using a different configuration file.

log.Stdout().Settings(filename).Info().Log("with settings overwritten")

Transactions

A transaction can be used to group related logs together.

logTx := log.BeginTx()

logTx.Append(log.Stdout().Info().Msg("first transaction entry"))
logTx.Append(log.File(filename).Error().Msgf("encountered error: %s", "sample error"))
logTx.Log()

Transactions also support metadata.

logTx := log.BeginTxWithMetadata(map[any]any{"something":"clean"})

logTx.Append(log.Stdout().Info().Msg("first transaction entry"))
logTx.Append(log.File(filename).Error().Msg("second line is an error"))
logTx.Log()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages