mwat56 / apachelogger

An Apache style file logger for the Go web-server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ApacheLogger

Golang GoDoc Go Report Issues Size Tag License View examples


Purpose

This package can be used to add a logfile facility to your Go web-server. The format of the generated logfile entries resemble those of the popular Apache web-server (see below).

Installation

You can use Go to install this package for you:

go get -u github.com/mwat56/apachelogger

Usage

To include the automatic logging facility you just call the Wrap() function as shown here:

func main() {
	// the filenames should be taken from the commandline
	// or a config file:
	accessLog := "/dev/stdout"
	errorLog := "/dev/stderr"

	pageHandler := http.NewServeMux()
	pageHandler.HandleFunc("/", myHandler)

	server := http.Server{
		Addr:    "127.0.0.1:8080",
		Handler: apachelogger.Wrap(pageHandler, accessLog, errorLog),
		//       ^^^^^^^^^^^^^^^^^^
	}
	apachelogger.SetErrLog(&server)

	if err := server.ListenAndServe(); nil != err {
		apachelogger.Close()
		log.Fatalf("%s: %v", os.Args[0], err)
	}
} // main()

So you just have to find a way the get/set the name of the desired logfile names – e.g. via a commandline option, or an environment variable, or a config file, whatever suits you best. Then you set up your server like shown above using the call to apachelogger.Wrap() to wrap your original pagehandler with the logging facility.

The creation pattern for a logfile entry is this:

apacheFormatPattern = `%s - %s [%s] "%s %s %s" %d %d "%s" "%s"`

All the placeholders to be seen in the pattern will be filled in with the appropriate values at runtime which are (in order of appearance):

  • remote IP,
  • remote user,
  • date/time of request,
  • request method,
  • requested URL,
  • request protocol,
  • server status,
  • served size,
  • remote referrer,
  • remote user agent.

It means you can now use all the logfile analysers etc. for Apache logs for your own logfiles as well.

Special Features

As privacy becomes a serious concern for a growing number of people (including law makers) – the IP address is definitely to be considered as personal data – this logging facility anonymises the requesting users by setting the host-part of the respective remote address to zero (0). This option takes care of e.g. European servers who may not without explicit consent of the users store personal data; this includes IP addresses in logfiles and elsewhere (eg. statistical data gathered from logfiles).

For debugging purposes there's a global flag AnonymiseErrors (default: false) that allows to fully (e.g. not anonymised) log all requests that cause errors (e.g. 4xx and 5xx statuses).

While the logging of web-requests is done automatically you can manually add entries to the logfile by calling

apachelogger.Log(aSender, aMessage string)

The aSender argument should give some indication of from where in your program you're calling the function, and aMessage is the text you want to write to the logfile. To preserve the format of the log-entry neither aSender nor aMessage should contain double-quotes ("). The messages are logged as coming from 127.0.0.1 with an user-agent of mwat56/apachelogger; this should make it easy to find these messages amongst all the 'normal' ones.

If you want to automatically log your server's errors as well you'd call

apachelogger.SetErrLog(aServer *http.Server)

during initialisation of your program. This will write the errors thrown by the server to the errorlog passed to the Wrap() function. Additionally you can call

apachelogger.Err(aSender, aMessage string)

from your own code to write a message to the error log.

To avoid that a panic crashes your program this module catches and recovers such situations. The error/cause of the panic is written to the error logfile for later inspection.

Libraries

No external libraries were used building ApacheLogger.

Licence

    Copyright © 2019, 2022 M.Watermann, 10247 Berlin, Germany
                    All rights reserved
                EMail : <support@mwat.de>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

You should have received a copy of the GNU General Public License along with this program. If not, see the GNU General Public License for details.


About

An Apache style file logger for the Go web-server

License:GNU General Public License v3.0


Languages

Language:Go 100.0%