useful

package
v0.0.0-...-9fb3cb1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2015 License: Apache-2.0, MIT Imports: 15 Imported by: 0

README

UsefulHandler

UsefulHandler is useful middleware for Go that will both compress

Documentation

Index

Constants

View Source
const (
	Byte     = 1
	B        = Byte
	Kilobyte = 1024 * Byte
	KB       = Kilobyte
	Megabyte = 1024 * Kilobyte
	MB       = Megabyte
	Gigabyte = 1024 * Megabyte
	GB       = Gigabyte
	Terabyte = 1024 * Gigabyte
	TB       = Terabyte
)

For ease of use.

View Source
const (
	Stdout dest = iota
	Stderr
	File
	Both
)

Locations for log writing.

Variables

View Source
var (
	CommonLog          = commonLog{commonLogFmt}
	CommonLogWithVHost = commonLogWithVHost{commonLogWithVHostFmt}
	NCSALog            = ncsaLog{ncsaLogFmt}
	RefererLog         = refererLog{refererLogFmt}
	AgentLog           = agentLog{agentLogFmt}
)

Log format types.

View Source
var ErrUnHijackable = errors.New("A(n) underlying ResponseWriter doesn't support the http.Hijacker interface")

ErrUnHijackable indicates an unhijackable connection. I.e., (one of) the underlying http.ResponseWriter(s) doesn't support the http.Hijacker interface.

Functions

func NewUsefulHandler

func NewUsefulHandler(handler http.Handler) http.Handler

NewUsefulHandler returns a *Handler with logging capabilities as well as potentially compressed content.

Types

type ApacheLogRecord

type ApacheLogRecord struct {
	http.ResponseWriter
	LogFmt
	// contains filtered or unexported fields
}

ApacheLogRecord is a structure containing the necessary information to write a proper log in the ApacheFormatPattern.

func (*ApacheLogRecord) Hijack

func (a *ApacheLogRecord) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error)

Hijack implements the http.Hijacker interface to allow connection hijacking.

func (*ApacheLogRecord) Log

func (r *ApacheLogRecord) Log(out io.Writer)

Log will log an entry to the io.Writer specified by LogDestination.

func (*ApacheLogRecord) Write

func (r *ApacheLogRecord) Write(p []byte) (int, error)

Write fulfills the Write method of the http.ResponseWriter interface.

func (*ApacheLogRecord) WriteHeader

func (r *ApacheLogRecord) WriteHeader(status int)

WriteHeader fulfills the WriteHeader method of the http.ResponseWriter interface.

type Handler

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

Handler is a wrapper around http.Handler in order for us to be able to fulfill the http.Handler interface. (The interface requires a ServeHTTP method which we cannot provide without defining our own type.)

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP fulfills the ServeHTTP method of the http.Handler interface.

type Log

type Log struct {
	Opts *Options // Current log options.

	*sync.Mutex // Mutex for locking.
	// contains filtered or unexported fields
}

Log is a structure with our open file we log to, the size of said file (measured by the number of bytes written to it, or it's size on initialization), our current writer (usually Stdout and the aforementioned file), our pool of random names, and a mutex lock to keep race conditions from tripping us up.

var (
	// LogFile is the active Log.
	LogFile *Log
)

func NewLog

func NewLog(opts *Options) (*Log, error)

NewLog returns a new Log initialized to the default values. If no log file exists with the name specified in 'LogName' it'll create a new one, otherwise it opens 'LogName'. If it cannot create or open a file it'll return nil for *Log and the applicable error.

func (*Log) Close

func (l *Log) Close()

Close closes the Log file.

func (*Log) Init

func (l *Log) Init(opts *Options)

Init sets LogFile and starts the check for 'cur'.

func (*Log) Rotate

func (l *Log) Rotate()

Rotate will rotate the logs so that the current (theoretically full) log will be compressed and added to the archive and a new log generated. Will panic if we cannot replace our physical file. (See newFile function for more information.)

func (*Log) SetWriter

func (l *Log) SetWriter()

SetWriter sets Log's writer depending on LogDestination.

func (*Log) Start

func (l *Log) Start()

Start begins the check for 'cur'. TOOD: Implement this better.

type LogFmt

type LogFmt interface {
	Print(w io.Writer, r *ApacheLogRecord) int
}

LogFmt is the interface implemented by log types to print an ApacheLogRecord in the desired format.

type Options

type Options struct {
	// LogFormat determines the format of the log. Most standard
	// formats found in Apache's mod_log_config docs are supported.
	LogFormat LogFmt

	// LogDestination determines where the Handler will write to.
	// By default it writes to Stdout and LogName.
	LogDestination dest

	// LogName is the name of the log the handler will write to.
	// It defaults to "access.log", but can be set to anything you
	// want.
	LogName string

	// ArchiveDir is the directory where the archives will be stored.
	// If set to "" (empty string) it'll be set to the current directory.
	// It defaults to "archives", so it'll look a little something like
	// this: '/home/user/files/archives/'
	ArchiveDir string

	// MaxFileSize is the maximum size of a log file in bytes.
	// It defaults to 1 Gigabyte (multiple of 1024, not 1000),
	// but can be set to anything you want.
	//
	// Log files larger than this size will be compressed into
	// archive files.
	MaxFileSize int64
}

Options is the structure containing the options for a given Log.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns the default configuration for the Options structure.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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