Giter Club home page Giter Club logo

nginx-http-json-log's Introduction

JSON format log for nginx

Description

nginx by default produces access logs in a flat file in plain text. This is sometimes inconvenient for log processors to work with.

I saw some people solve the problem by manually construct a json template in the log\_format directive. This is not only inconvenient but also potential to generate malformed json strings.

So I created this module to mimic the http log module while producing json format logs when written to file.

Dependencies

This module require libjansson (>= 2.5.0)

For ubuntu you can run

sudo apt-get install libjansson-dev

to install the dependency.

How to build

Assume you clone the source code in ~/workspace/nginx-http-json-log folder.

  1. Get a copy of nginx source code in ~/workspace/nginx
  2. cd to ~/workspace/nginx
  3. Run ./auto/config [other options] --add-module='../nginx-http-json-log'
  4. Run make

How to use

After you build nginx with this module, you can add several directives in the nginx.conf file to config the json log fields and the log file path.

There are 2 new directives introduced by this module

access_json_log

The directive is used to specify the log path and the json fields, which is similar to access_log directive in the nginx http log module.

To turn off the json log, you can specify access_json_log off.

json_log_fields

This directive is used to select which variables you want to log in the log file, which is similar to log_format in the nginx http log module.

Sample config

http {
    include       mime.types;
    default_type  application/octet-stream;

    json_log_fields main 'remote_addr'
                         'remote_user'
                         'request'
                         'time_local'
                         'status'
                         'body_bytes_sent'
                         'http_user_agent'
                         'http_x_forwarded_for';

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        access_json_log logs/access_json.log main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}

Output in log file:

{
  "http_x_forwarded_for": "-",
  "http_user_agent": "ApacheBench/2.3",
  "body_bytes_sent": "612",
  "status": "200",
  "remote_user": "-",
  "remote_addr": "127.0.0.1",
  "time_local": "20/Jul/2015:23:48:14 -0700",
  "request": "GET / HTTP/1.0"
}

nginx-http-json-log's People

Watchers

James Cloos avatar  avatar

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.