1 17 package org.apache.log.format; 18 19 import org.apache.log.LogEvent; 20 import org.apache.log.Priority; 21 22 28 public class SyslogFormatter 29 implements Formatter 30 { 31 public static final int PRIORITY_DEBUG = 7; 32 public static final int PRIORITY_INFO = 6; 33 public static final int PRIORITY_NOTICE = 5; 34 public static final int PRIORITY_WARNING = 4; 35 public static final int PRIORITY_ERR = 3; 36 public static final int PRIORITY_CRIT = 2; 37 public static final int PRIORITY_ALERT = 1; 38 public static final int PRIORITY_EMERG = 0; 39 40 43 public static final int FACILITY_KERN = ( 0 << 3 ); 44 public static final int FACILITY_USER = ( 1 << 3 ); 45 public static final int FACILITY_MAIL = ( 2 << 3 ); 46 public static final int FACILITY_DAEMON = ( 3 << 3 ); 47 public static final int FACILITY_AUTH = ( 4 << 3 ); 48 public static final int FACILITY_SYSLOG = ( 5 << 3 ); 49 public static final int FACILITY_LPR = ( 6 << 3 ); 50 public static final int FACILITY_NEWS = ( 7 << 3 ); 51 public static final int FACILITY_UUCP = ( 8 << 3 ); 52 public static final int FACILITY_CRON = ( 9 << 3 ); 53 public static final int FACILITY_AUTHPRIV = ( 10 << 3 ); 54 public static final int FACILITY_FTP = ( 11 << 3 ); 55 56 public static final int FACILITY_LOCAL0 = ( 16 << 3 ); 57 public static final int FACILITY_LOCAL1 = ( 17 << 3 ); 58 public static final int FACILITY_LOCAL2 = ( 18 << 3 ); 59 public static final int FACILITY_LOCAL3 = ( 19 << 3 ); 60 public static final int FACILITY_LOCAL4 = ( 20 << 3 ); 61 public static final int FACILITY_LOCAL5 = ( 21 << 3 ); 62 public static final int FACILITY_LOCAL6 = ( 22 << 3 ); 63 public static final int FACILITY_LOCAL7 = ( 23 << 3 ); 64 65 protected static final String [] FACILITY_DESCRIPTIONS = 67 { 68 "kern", "user", "mail", "daemon", "auth", "syslog", 69 "lpr", "news", "uucp", "cron", "authpriv", "ftp", 70 "", "", "", "", "local0", "local1", "local2", "local3", 71 "local4", "local5", "local6", "local7" 72 }; 73 74 private int m_facility; 76 77 private boolean m_showFacilityBanner; 79 80 83 public SyslogFormatter() 84 { 85 this( FACILITY_USER ); 86 } 87 88 93 public SyslogFormatter( final int facility ) 94 { 95 this( facility, true ); 96 } 97 98 104 public SyslogFormatter( final int facility, final boolean showFacilityBanner ) 105 { 106 m_facility = facility; 107 m_showFacilityBanner = showFacilityBanner; 108 } 109 110 116 public String format( final LogEvent event ) 117 { 118 final int priority = getSyslogPriority( event ); 119 final int facility = getSyslogFacility( event ); 120 String message = event.getMessage(); 121 122 124 if( null == message ) 125 { 126 message = ""; 127 } 128 129 if( m_showFacilityBanner ) 130 { 131 message = getFacilityDescription( facility ) + ": " + message; 132 } 133 134 return "<" + ( facility | priority ) + "> " + message; 135 } 136 137 143 protected String getFacilityDescription( final int facility ) 144 { 145 return FACILITY_DESCRIPTIONS[ facility >> 3 ]; 146 } 147 148 155 protected int getSyslogFacility( final LogEvent event ) 156 { 157 return m_facility; 158 } 159 160 167 protected int getSyslogPriority( final LogEvent event ) 168 { 169 if( event.getPriority().isLowerOrEqual( Priority.DEBUG ) ) 170 { 171 return PRIORITY_DEBUG; 172 } 173 else if( event.getPriority().isLowerOrEqual( Priority.INFO ) ) 174 { 175 return PRIORITY_INFO; 176 } 177 else if( event.getPriority().isLowerOrEqual( Priority.WARN ) ) 178 { 179 return PRIORITY_WARNING; 180 } 181 else if( event.getPriority().isLowerOrEqual( Priority.ERROR ) ) 182 { 183 return PRIORITY_ERR; 184 } 185 else 186 { 187 return PRIORITY_CRIT; 188 } 189 } 190 } 191 | Popular Tags |