pglogrus

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2017 License: MIT Imports: 7 Imported by: 0

README

PostgreSQL Hook for Logrus  Build Status godoc reference

Use this hook to send your logs to postgresql server.

Usage

The hook must be configured with:

  • A postgresql db connection (**sql.DB)
  • an optional hash with extra global fields. These fields will be included in all messages sent to postgresql
package main

import (
    log "github.com/sirupsen/logrus"
    "gopkg.in/gemnasium/logrus-postgresql-hook.v1"
    )

func main() {
    db, err := sql.Open("postgres", "user=postgres dbname=postgres host=postgres sslmode=disable")
      if err != nil {
        t.Fatal("Can't connect to postgresql database:", err)
      }
    defer db.Close()
    hook := pglorus.NewHook(db, map[string]interface{}{"this": "is logged every time"})
    log.Hooks.Add(hook)
    log.Info("some logging message")
}
Asynchronous logger

This package provides an asynchronous hook, so logging won't block waiting for the data to be inserted in the DB. Be careful to defer call hook.Flush() if you are using this kind of hook.

package main

import (
    log "github.com/sirupsen/logrus"
    "gopkg.in/gemnasium/logrus-postgresql-hook.v1"
    )

func main() {
    db, err := sql.Open("postgres", "user=postgres dbname=postgres host=postgres sslmode=disable")
      if err != nil {
        t.Fatal("Can't connect to postgresql database:", err)
      }
    defer db.Close()
    hook := pglorus.NewAsyncHook(db, map[string]interface{}{"this": "is logged every time"})
    defer hook.Flush()
    log.Hooks.Add(hook)
    log.Info("some logging message")
}
Customize insertion

By defaults, the hook will log into a logs table (cf the test schema in migrations). To change this behavior, set the InsertFunc of the hook:

package main

import (
    log "github.com/sirupsen/logrus"
    "gopkg.in/gemnasium/logrus-postgresql-hook.v1"
    )

func main() {
    db, err := sql.Open("postgres", "user=postgres dbname=postgres host=postgres sslmode=disable")
      if err != nil {
        t.Fatal("Can't connect to postgresql database:", err)
      }
    defer db.Close()

    hook := pglorus.NewHook(db, map[string]interface{}{"this": "is logged every time"})
    hook.InsertFunc = func(db *sqlDB, entry *logrus.Entry) error {
      jsonData, err := json.Marshal(entry.Data)
        if err != nil {
          return err
        }

      _, err = db.Exec("INSERT INTO another_logs_table(level, message, message_data, created_at) VALUES ($1,$2,$3,$4);", entry.Level, entry.Message, jsonData, entry.Time)
        return err
    }
    log.Hooks.Add(hook)
    log.Info("some logging message")
}

Run tests

Since this hook is hitting a DB, we're testing again a real PostgreSQL server:

docker-compose run --rm test

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BufSize uint = 8192

Set pglogrus.BufSize = <value> _before_ calling NewHook Once the buffer is full, logging will start blocking, waiting for slots to be available in the queue.

Functions

This section is empty.

Types

type AsyncHook

type AsyncHook struct {
	*Hook

	Ticker     *time.Ticker
	InsertFunc func(*sql.Tx, *logrus.Entry) error
	// contains filtered or unexported fields
}

func NewAsyncHook

func NewAsyncHook(db *sql.DB, extra map[string]interface{}) *AsyncHook

NewAsyncHook creates a hook to be added to an instance of logger. The hook created will be asynchronous, and it's the responsibility of the user to call the Flush method before exiting to empty the log queue.

func (*AsyncHook) Fire

func (hook *AsyncHook) Fire(entry *logrus.Entry) error

Fire is called when a log event is fired. We assume the entry will be altered by another hook, otherwise we might logging something wrong to PostgreSQL

func (*AsyncHook) Flush

func (hook *AsyncHook) Flush()

Flush waits for the log queue to be empty. This func is meant to be used when the hook was created with NewAsyncHook.

type Hook

type Hook struct {
	Extra map[string]interface{}

	InsertFunc func(*sql.DB, *logrus.Entry) error
	// contains filtered or unexported fields
}

Hook to send logs to a PostgreSQL database

func NewHook

func NewHook(db *sql.DB, extra map[string]interface{}) *Hook

NewHook creates a PGHook to be added to an instance of logger.

func (*Hook) Blacklist

func (hook *Hook) Blacklist(b []string)

Blacklist creates a blacklist map to filter some message keys. This useful when you want your application to log extra fields locally but don't want pg to store them.

func (*Hook) Close added in v1.0.2

func (hook *Hook) Close() error

func (*Hook) Fire

func (hook *Hook) Fire(entry *logrus.Entry) error

func (*Hook) Levels

func (hook *Hook) Levels() []logrus.Level

Levels returns the available logging levels.

Jump to

Keyboard shortcuts

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