src/pkg/log/syslog/syslog.go - The Go Programming Language

Golang

Source file src/pkg/log/syslog/syslog.go

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// +build !windows,!plan9
     6	
     7	// Package syslog provides a simple interface to the system log service. It
     8	// can send messages to the syslog daemon using UNIX domain sockets, UDP, or
     9	// TCP connections.
    10	package syslog
    11	
    12	import (
    13		"errors"
    14		"fmt"
    15		"log"
    16		"net"
    17		"os"
    18	)
    19	
    20	type Priority int
    21	
    22	const (
    23		// From /usr/include/sys/syslog.h.
    24		// These are the same on Linux, BSD, and OS X.
    25		LOG_EMERG Priority = iota
    26		LOG_ALERT
    27		LOG_CRIT
    28		LOG_ERR
    29		LOG_WARNING
    30		LOG_NOTICE
    31		LOG_INFO
    32		LOG_DEBUG
    33	)
    34	
    35	// A Writer is a connection to a syslog server.
    36	type Writer struct {
    37		priority Priority
    38		prefix   string
    39		conn     serverConn
    40	}
    41	
    42	type serverConn interface {
    43		writeBytes(p Priority, prefix string, b []byte) (int, error)
    44		writeString(p Priority, prefix string, s string) (int, error)
    45		close() error
    46	}
    47	
    48	type netConn struct {
    49		conn net.Conn
    50	}
    51	
    52	// New establishes a new connection to the system log daemon.
    53	// Each write to the returned writer sends a log message with
    54	// the given priority and prefix.
    55	func New(priority Priority, prefix string) (w *Writer, err error) {
    56		return Dial("", "", priority, prefix)
    57	}
    58	
    59	// Dial establishes a connection to a log daemon by connecting
    60	// to address raddr on the network net.
    61	// Each write to the returned writer sends a log message with
    62	// the given priority and prefix.
    63	func Dial(network, raddr string, priority Priority, prefix string) (w *Writer, err error) {
    64		if prefix == "" {
    65			prefix = os.Args[0]
    66		}
    67		var conn serverConn
    68		if network == "" {
    69			conn, err = unixSyslog()
    70		} else {
    71			var c net.Conn
    72			c, err = net.Dial(network, raddr)
    73			conn = netConn{c}
    74		}
    75		return &Writer{priority, prefix, conn}, err
    76	}
    77	
    78	// Write sends a log message to the syslog daemon.
    79	func (w *Writer) Write(b []byte) (int, error) {
    80		if w.priority > LOG_DEBUG || w.priority < LOG_EMERG {
    81			return 0, errors.New("log/syslog: invalid priority")
    82		}
    83		return w.conn.writeBytes(w.priority, w.prefix, b)
    84	}
    85	
    86	func (w *Writer) writeString(p Priority, s string) (int, error) {
    87		return w.conn.writeString(p, w.prefix, s)
    88	}
    89	
    90	func (w *Writer) Close() error { return w.conn.close() }
    91	
    92	// Emerg logs a message using the LOG_EMERG priority.
    93	func (w *Writer) Emerg(m string) (err error) {
    94		_, err = w.writeString(LOG_EMERG, m)
    95		return err
    96	}
    97	
    98	// Alert logs a message using the LOG_ALERT priority.
    99	func (w *Writer) Alert(m string) (err error) {
   100		_, err = w.writeString(LOG_ALERT, m)
   101		return err
   102	}
   103	
   104	// Crit logs a message using the LOG_CRIT priority.
   105	func (w *Writer) Crit(m string) (err error) {
   106		_, err = w.writeString(LOG_CRIT, m)
   107		return err
   108	}
   109	
   110	// Err logs a message using the LOG_ERR priority.
   111	func (w *Writer) Err(m string) (err error) {
   112		_, err = w.writeString(LOG_ERR, m)
   113		return err
   114	}
   115	
   116	// Warning logs a message using the LOG_WARNING priority.
   117	func (w *Writer) Warning(m string) (err error) {
   118		_, err = w.writeString(LOG_WARNING, m)
   119		return err
   120	}
   121	
   122	// Notice logs a message using the LOG_NOTICE priority.
   123	func (w *Writer) Notice(m string) (err error) {
   124		_, err = w.writeString(LOG_NOTICE, m)
   125		return err
   126	}
   127	
   128	// Info logs a message using the LOG_INFO priority.
   129	func (w *Writer) Info(m string) (err error) {
   130		_, err = w.writeString(LOG_INFO, m)
   131		return err
   132	}
   133	
   134	// Debug logs a message using the LOG_DEBUG priority.
   135	func (w *Writer) Debug(m string) (err error) {
   136		_, err = w.writeString(LOG_DEBUG, m)
   137		return err
   138	}
   139	
   140	func (n netConn) writeBytes(p Priority, prefix string, b []byte) (int, error) {
   141		_, err := fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, b)
   142		if err != nil {
   143			return 0, err
   144		}
   145		return len(b), nil
   146	}
   147	
   148	func (n netConn) writeString(p Priority, prefix string, s string) (int, error) {
   149		_, err := fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, s)
   150		if err != nil {
   151			return 0, err
   152		}
   153		return len(s), nil
   154	}
   155	
   156	func (n netConn) close() error {
   157		return n.conn.Close()
   158	}
   159	
   160	// NewLogger creates a log.Logger whose output is written to
   161	// the system log service with the specified priority. The logFlag
   162	// argument is the flag set passed through to log.New to create
   163	// the Logger.
   164	func NewLogger(p Priority, logFlag int) (*log.Logger, error) {
   165		s, err := New(p, "")
   166		if err != nil {
   167			return nil, err
   168		}
   169		return log.New(s, "", logFlag), nil
   170	}