LogEntry.java Source code

Java tutorial

Introduction

Here is the source code for LogEntry.java

Source

/*
 * Java-based syslog server and log search engine
 * Copyright (C) 2011 Henning Peters <pete@dexterslab.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 program 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.apache.lucene.document.Document;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LogEntry {

    private StringTemplateGroup templates = null;

    static int nextId = 0;

    public long id;
    public String host;
    public long timestamp;
    public int facility;
    public int priority;
    public String message;

    // optional
    public String timestampString;
    public String facilityString;
    public String priorityString;

    public LogEntry(long myId, String myHostname, long myTimestamp, String myMessage, int myFacility,
            int myPriority) {
        id = myId;
        host = myHostname;

        timestamp = myTimestamp;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
        timestampString = formatter.format(calendar.getTime());

        facility = myFacility;
        if (facility >= 0 && facility < facilityStrings.length) {
            facilityString = facilityStrings[facility];
        } else {
            facilityString = "unknown";
        }

        priority = myPriority;
        if (priority >= 0 && priority < priorityStrings.length) {
            priorityString = priorityStrings[priority];
        } else {
            priorityString = "unknown";
        }

        message = myMessage;
    }

    public LogEntry(Document doc) {
        this(Long.parseLong(doc.get("id"), 10), doc.get("host"), Long.parseLong(doc.get("timestamp"), 10),
                doc.get("message"), Integer.parseInt(doc.get("facility"), 10),
                Integer.parseInt(doc.get("priority"), 10));
    }

    public LogEntry(String myHostname, String myMessage, int myFacility, int myPriority) {
        this(nextId++, myHostname, System.currentTimeMillis(), myMessage, myFacility, myPriority);
    }

    public LogEntry(String myHostname, String myMessage) {
        this(myHostname, myMessage, -1, -1);
    }

    public String[] facilityStrings = { "kern", "user", "mail", "daemon", "auth", "syslog", "lpr", "news", "uucp",
            "cron", "authpriv", "ftp", "ntp", "security", "console", "mark", "local0", "local1", "local2", "local3",
            "local4", "local5", "local6", "local7" };

    public String[] priorityStrings = { "emerg", "alert", "crit", "err", "warning", "notice", "info", "debug" };

    public String encode() {
        return "<" + ((facility << 3) + priority) + ">" + message;
    }

    @Override
    public String toString() {
        return "id: " + id + " host:" + host + " timestamp:" + timestampString + " (" + timestamp + ") facility:"
                + facilityString + " (" + facility + ") priority:" + priorityString + " (" + priority + ") message:"
                + message;
    }

    public JSONObject toJSON() {
        if (templates == null) {
            templates = new StringTemplateGroup("templates", "templates");
        }

        StringTemplate t = templates.getInstanceOf("entry");
        t.setAttribute("entry", this);

        JSONObject json = new JSONObject();
        try {
            JSONObject event = new JSONObject();
            event.put("id", id);
            event.put("html", t.toString());

            JSONArray events = new JSONArray();
            events.put(event);

            json.put("events", events);
            json.put("cursor", id + 1);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }

        return json;
    }
}