001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log.format;
056:
057: import org.apache.log.LogEvent;
058: import org.apache.log.Priority;
059:
060: /**
061: * A formatter that serializes in the format originally
062: * used by BSD syslog daemon.
063: *
064: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
065: */
066: public class SyslogFormatter implements Formatter,
067: org.apache.log.Formatter {
068: public static final int PRIORITY_DEBUG = 7;
069: public static final int PRIORITY_INFO = 6;
070: public static final int PRIORITY_NOTICE = 5;
071: public static final int PRIORITY_WARNING = 4;
072: public static final int PRIORITY_ERR = 3;
073: public static final int PRIORITY_CRIT = 2;
074: public static final int PRIORITY_ALERT = 1;
075: public static final int PRIORITY_EMERG = 0;
076:
077: /*
078: * Constants for facility.
079: */
080: public static final int FACILITY_KERN = (0 << 3);
081: public static final int FACILITY_USER = (1 << 3);
082: public static final int FACILITY_MAIL = (2 << 3);
083: public static final int FACILITY_DAEMON = (3 << 3);
084: public static final int FACILITY_AUTH = (4 << 3);
085: public static final int FACILITY_SYSLOG = (5 << 3);
086: public static final int FACILITY_LPR = (6 << 3);
087: public static final int FACILITY_NEWS = (7 << 3);
088: public static final int FACILITY_UUCP = (8 << 3);
089: public static final int FACILITY_CRON = (9 << 3);
090: public static final int FACILITY_AUTHPRIV = (10 << 3);
091: public static final int FACILITY_FTP = (11 << 3);
092:
093: public static final int FACILITY_LOCAL0 = (16 << 3);
094: public static final int FACILITY_LOCAL1 = (17 << 3);
095: public static final int FACILITY_LOCAL2 = (18 << 3);
096: public static final int FACILITY_LOCAL3 = (19 << 3);
097: public static final int FACILITY_LOCAL4 = (20 << 3);
098: public static final int FACILITY_LOCAL5 = (21 << 3);
099: public static final int FACILITY_LOCAL6 = (22 << 3);
100: public static final int FACILITY_LOCAL7 = (23 << 3);
101:
102: ///String descriptions of all the facilities
103: protected static final String[] FACILITY_DESCRIPTIONS = { "kern",
104: "user", "mail", "daemon", "auth", "syslog", "lpr", "news",
105: "uucp", "cron", "authpriv", "ftp", "", "", "", "",
106: "local0", "local1", "local2", "local3", "local4", "local5",
107: "local6", "local7" };
108:
109: ///Constant for holding facility id
110: private int m_facility;
111:
112: ///flag to decide whether we write out Facility banner
113: private boolean m_showFacilityBanner;
114:
115: /**
116: * Constructor that assumes FACILITY_USER.
117: */
118: public SyslogFormatter() {
119: this (FACILITY_USER);
120: }
121:
122: /**
123: * Constructor so that you can associate facility with formatter.
124: *
125: * @param facility the facility constant
126: */
127: public SyslogFormatter(final int facility) {
128: this (facility, true);
129: }
130:
131: /**
132: * Constructor allowing setting of facility and whether to show banner.
133: *
134: * @param facility the facility code.
135: * @param showFacilityBanner true if facility banner should be shown
136: */
137: public SyslogFormatter(final int facility,
138: final boolean showFacilityBanner) {
139: m_facility = facility;
140: m_showFacilityBanner = showFacilityBanner;
141: }
142:
143: /**
144: * Format log event into syslog string.
145: *
146: * @param event the event
147: * @return the formatted string
148: */
149: public String format(final LogEvent event) {
150: final int priority = getSyslogPriority(event);
151: final int facility = getSyslogFacility(event);
152: String message = event.getMessage();
153:
154: //TODO: Clean and spruce message here (ie remove \t and \n's)
155:
156: if (null == message) {
157: message = "";
158: }
159:
160: if (m_showFacilityBanner) {
161: message = getFacilityDescription(facility) + ": " + message;
162: }
163:
164: return "<" + (facility | priority) + "> " + message;
165: }
166:
167: /**
168: * Retrieve description for facility.
169: *
170: * @param facility the facility code
171: * @return the facility description
172: */
173: protected String getFacilityDescription(final int facility) {
174: return FACILITY_DESCRIPTIONS[facility >> 3];
175: }
176:
177: /**
178: * Get facility associated with event.
179: * Default implementation returns facility set in constructor.
180: *
181: * @param event the log event
182: * @return the facility code
183: */
184: protected int getSyslogFacility(final LogEvent event) {
185: return m_facility;
186: }
187:
188: /**
189: * Get syslog priority code for LogEvent.
190: * This is done by translating LogKit priority to syslog priority.
191: *
192: * @param event the log event
193: * @return the priority code
194: */
195: protected int getSyslogPriority(final LogEvent event) {
196: if (event.getPriority().isLowerOrEqual(Priority.DEBUG))
197: return PRIORITY_DEBUG;
198: else if (event.getPriority().isLowerOrEqual(Priority.INFO))
199: return PRIORITY_INFO;
200: else if (event.getPriority().isLowerOrEqual(Priority.WARN))
201: return PRIORITY_WARNING;
202: else if (event.getPriority().isLowerOrEqual(Priority.ERROR))
203: return PRIORITY_ERR;
204: else
205: return PRIORITY_CRIT;
206: }
207: }
|