gomoljson

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2018 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gomol-json

GoDoc Build Status Code Coverage

gomol-json is a logger for gomol to support logging JSON over a network.

Installation

To install gomol-json, simply go get it and import it into your project:

go get github.com/aphistic/gomol-json
...
import "github.com/aphistic/gomol-json"

Examples

For brevity a lot of error checking has been omitted, be sure you do your checks!

This is a super basic example of adding a JSON logger to gomol and then logging a few messages:

package main

import (
	"github.com/aphistic/gomol"
)

// Create a new JSON logger, add it to gomol and log a few messages
func Example() {
	// Add a JSON Logger
	jsonCfg := NewJSONLoggerConfig("tcp://10.10.10.10:1234")
	jsonLogger, _ := NewJSONLogger(jsonCfg)
	gomol.AddLogger(jsonLogger)

	// Set some global attrs that will be added to all
	// messages automatically
	gomol.SetAttr("facility", "gomol.example")
	gomol.SetAttr("another_attr", 1234)

	// Initialize the loggers
	gomol.InitLoggers()
	defer gomol.ShutdownLoggers()

	// Log some debug messages with message-level attrs
	// that will be sent only with that message
	for idx := 1; idx <= 10; idx++ {
		gomol.Dbgm(
			gomol.NewAttrs().
				SetAttr("msg_attr1", 4321),
			"Test message %v", idx)
	}
}

Documentation

Overview

Package gomoljson is a JSON logger implementation for gomol.

Message order is not guaranteed during reconnection scenarios.

Example

Create a new JSON logger, add it to gomol and log a few messages

// Add a JSON Logger
jsonCfg := NewJSONLoggerConfig("tcp://10.10.10.10:1234")
jsonLogger, _ := NewJSONLogger(jsonCfg)
gomol.AddLogger(jsonLogger)

// Set some global attrs that will be added to all
// messages automatically
gomol.SetAttr("facility", "gomol.example")
gomol.SetAttr("another_attr", 1234)

// Initialize the loggers
gomol.InitLoggers()
defer gomol.ShutdownLoggers()

// Log some debug messages with message-level attrs
// that will be sent only with that message
for idx := 1; idx <= 10; idx++ {
	gomol.Dbgm(
		gomol.NewAttrs().
			SetAttr("msg_attr1", 4321),
		"Test message %v", idx)
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrDisconnected = newGomolJsonError("disconnected", false, false)
)

Functions

This section is empty.

Types

type GomolJsonError

type GomolJsonError struct {
	// contains filtered or unexported fields
}

func (*GomolJsonError) Error

func (gje *GomolJsonError) Error() string

func (*GomolJsonError) Temporary

func (gje *GomolJsonError) Temporary() bool

func (*GomolJsonError) Timeout

func (gje *GomolJsonError) Timeout() bool

type JSONLogger

type JSONLogger struct {
	// contains filtered or unexported fields
}

JSONLogger is an instance of a JSON logger

func NewJSONLogger

func NewJSONLogger(config *JSONLoggerConfig) (*JSONLogger, error)

NewJSONLogger creates a new logger with the provided configuration

func (*JSONLogger) Healthy

func (l *JSONLogger) Healthy() bool

Healthy will return true if the logger is connected to the remote host

func (*JSONLogger) InitLogger

func (l *JSONLogger) InitLogger() error

InitLogger does any initialization the logger may need before being used

func (*JSONLogger) IsInitialized

func (l *JSONLogger) IsInitialized() bool

IsInitialized returns whether the logger has already been initialized or not

func (*JSONLogger) Logm

func (l *JSONLogger) Logm(timestamp time.Time, level gomol.LogLevel, attrs map[string]interface{}, msg string) error

Logm sends a JSON log message to the configured host

func (*JSONLogger) SetBase

func (l *JSONLogger) SetBase(base *gomol.Base)

SetBase will set the gomol.Base this logger is associated with

func (*JSONLogger) ShutdownLogger

func (l *JSONLogger) ShutdownLogger() error

ShutdownLogger shuts down the logger and frees any resources that may be used

type JSONLoggerConfig

type JSONLoggerConfig struct {

	// A URI for the host to connect to in the format: protocol://host:port. Ex: tcp://10.10.10.10:1234
	HostURI string

	// DialTimeout is the amount of time the logger will try to connect to the host before timing out
	DialTimeout time.Duration

	// The delimiter to use at the end of every message sent. Defaults to '\n'
	MessageDelimiter []byte

	// The prefix to add before every field name in the JSON data. Defaults to a blank string
	FieldPrefix string
	// A list of field names excluded from having the FieldPrefix added
	UnprefixedFields []string

	// The name of the JSON field to put the log level into. Defaults to "level"
	LogLevelField string
	// The name of the JSON field to put the message into. Defaults to "message"
	MessageField string
	// The name of the JSON field to put the timestamp into. Defaults to "timestamp"
	TimestampField string

	// A map to customize the values of each gomol.LogLevel in the JSON message.
	// Defaults to the string value of each gomol.LogLevel
	LogLevelMap map[gomol.LogLevel]interface{}

	// A map of additional attributes to be added to each JSON message sent. This is useful
	// if there fields to send only to a JSON receiver.  These will override any existing
	// attributes already set on a message.
	JSONAttrs map[string]interface{}

	// The number of messages to queue during a connection failure before
	// older messages will start being dropped. Defaults to 100
	FailureQueueLength int

	// Whether an active connection is required on initialization. Defaults to false.
	// If true, the Init function will return a nil error on connection failure, but
	// retry to connect in the background.
	AllowDisconnectedInit bool

	// The backoff strategy to use when reconnecting a connection
	ReconnectBackoff backoff.Backoff
	// contains filtered or unexported fields
}

JSONLoggerConfig is the configuration for a JSONLogger

func NewJSONLoggerConfig

func NewJSONLoggerConfig(hostURI string) *JSONLoggerConfig

NewJSONLoggerConfig creates a new configuration with default settings

type LoggerOption

type LoggerOption func(cfg *JSONLoggerConfig)

func WithDialTimeout

func WithDialTimeout(timeout time.Duration) LoggerOption

WithDialTimeout sets the amount of time the logger will try to connect to the host before timing out

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址