Giter Site home page Giter Site logo

good-apache-log's Introduction

Good-Apache-Log

Apache httpd log files for Hapi web servers

Lead Maintainer: Jason Smith

Overview

Good-Apache-Log makes Hapi write server logs in the server logs in the Apache combined or common log format, via the Good reporting framework.

The Apache format? You know: this one—the ubiquitous format every tool on the Internet can process.

127.0.0.1 - - [07/Aug/2015:14:01:21 +07:00] "GET / HTTP/1.1" 200 39 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12"
127.0.0.1 - - [07/Aug/2015:14:01:21 +07:00] "GET /favicon.ico HTTP/1.1" 200 39 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12"
127.0.0.1 - - [07/Aug/2015:14:01:31 +07:00] "GET /product/280 HTTP/1.1" 200 45 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36"
127.0.0.1 - - [07/Aug/2015:14:01:31 +07:00] "GET /favicon.ico HTTP/1.1" 200 41 "http://localhost:8080/product/280" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36"

Good-Apache-Log appends to a log file, and it it re-opens the log file when you send SIGHUP. This makes it very easy to implement log rotation, using logrotate, for example.

In other words, Good-Apache-Log makes your Hapi server a much more friendly dev-ops citizen by supporting standard logging formats and behavior.

Usage

Install this package with npm:

$ npm install good-apache-log

Place Good-Apache-Log as a reporter in your Good configuration.

var logFile = "my-server.log"
var reporters = [ {reporter:GoodApacheLog, events:{response:'*'}, config:logFile} ]
var goodPlugin = {register:Good, options:{responsePayload:true, reporters:reporters}}
server.register([goodPlugin], function(er) {
  if (!er)
    console.log('GoodApacheLog is registered.')
})

Now you will see a familiar face in my-server.log.

Note, if you set "responsePayload": true in the Good config, then your logs will show the response payload size (the %b format string). Otherwise, the payload size will be logged as -.

See test/server.js for an example of a very simple Hapi server that uses GoodApacheLog.

Configuration

Usually, you can set the reporter config value to the path to your log file, or to an existing stream (eg: console.stdout). Hapi will now automatically append logs to that file/stream, and for a file if you send a HUP signal to your server, it will re-open that file.

However, for more detailed control, config can be an object with these keys:

  • file - The path to the log file, or an existing stream
  • format - The log format to use, default is "combined"; see Log Formats below
  • separator - The separator between log lines, default is "\n"
  • hup - Boolean; if true, Good-Apache-Log will listen to SIGHUP and re-open its log file; default is true

Log Formats

Similar to the Apache httpd server, the format value allows you to specify your log file format. Either choose a "nickname" or provide your own format string.

Note Not all format strings are supported yet. If you need one, please create an issue or pull request.

For example to make a simple log file of status codes and user-agents:

var reporters = [
  {
    reporter: GoodApacheLog,
    events: {response:'*'},
    config: {file:"httpd.log", format:"%s %{User-agent}i"}
  }
]

Example log file:

200 Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0
404 Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4
500 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36
200 Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4
301 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36

Supported nicknames

These nicknames, taken from from the Apache httpd documentation, expand to the most commonly-used log formats.

  • combined: %h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"
  • common: %h %l %u %t "%r" %>s %b
  • referer: %{Referer}i -> %U

good-apache-log's People

Contributors

jhs avatar jdirwin avatar

Stargazers

Jedsada Tiwongvorakul avatar Abhijeet Shah avatar Dhi Aurrahman avatar

Watchers

James Cloos avatar  avatar

good-apache-log's Issues

No way to see %r, the first line of the request

The %r format string expands to the "first line of request", for example, GET /hello.html HTTP/1.1. Unfortunately, the Good API does not provide this string.

Worse, Good does not inform us the HTTP version at all. So for now it is hard-coded as HTTP/1.1.

If the response event does contain log events of the request, then the requested path is used from there. Otherwise it is built from the path and query objects in the response event.

Compute the JSON payload size

If your response payload is an object, Hapi will convert that to JSON after Good-Apache-Log logs the response. To compute the payload size, Good-Apache-Log will JSON.stringify() the payload and basically discard the result.

If you have very high load or very large JSON payloads, this might impart a small speed penalty.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.