Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.graylog2.syslog4j;
  
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
  
This class provides a Singleton interface for Syslog4j client implementations.

Usage examples:

Direct

 Syslog.getInstance("udp").info("log message");
 

Via Instance

 SyslogIF syslog = Syslog.getInstance("udp");
 syslog.info();
 

Syslog4j is licensed under the Lesser GNU Public License v2.1. A copy of the LGPL license is available in the META-INF folder in all distributions of Syslog4j and in the base directory of the "doc" ZIP.

Author(s):
<syslog4j@productivity.org>
Version:
$Id: Syslog.java,v 1.23 2011/01/23 20:49:12 cvs Exp $
 
 public final class Syslog implements SyslogConstants {
     private static final long serialVersionUID = -4662318148650646144L;
 
     private static boolean SUPPRESS_RUNTIME_EXCEPTIONS = false;
 
     protected static final Map instances = new Hashtable();
 
     static {
         initialize();
     }

    
Syslog is a singleton.
 
     private Syslog() {
         //
     }

    

Returns:
Returns the current version identifier for Syslog4j.
 
     public static final String getVersion() {
         return .;
     }

    

Parameters:
suppress - true to suppress throwing SyslogRuntimeException in many methods of this class, false to throw exceptions (default)
 
     public static void setSuppressRuntimeExceptions(boolean suppress) {
          = suppress;
     }

    

Returns:
Returns whether or not to suppress throwing SyslogRuntimeException in many methods of this class.
 
     public static boolean getSuppressRuntimeExceptions() {
         return ;
     }

    
Throws SyslogRuntimeException unless it has been suppressed via setSuppressRuntimeException(boolean).

Parameters:
message
Throws:
SyslogRuntimeException
 
     private static void throwRuntimeException(String messagethrows SyslogRuntimeException {
         if () {
             return;
 
         } else {
             throw new SyslogRuntimeException(message.toString());
         }
     }

    
Use getInstance(protocol) as the starting point for Syslog4j.

Parameters:
protocol - the Syslog protocol to use, e.g. "udp", "tcp", "unix_syslog", "unix_socket", or a custom protocol
Returns:
Returns an instance of SyslogIF.
Throws:
SyslogRuntimeException
 
     public static final SyslogIF getInstance(String protocolthrows SyslogRuntimeException {
        String _protocol = protocol.toLowerCase();
        if (.containsKey(_protocol)) {
            return (SyslogIF.get(_protocol);
        } else {
            StringBuffer message = new StringBuffer("Syslog protocol \"" + protocol + "\" not defined; call Syslogger.createSyslogInstance(protocol,config) first");
            if (.size() > 0) {
                message.append(" or use one of the following instances: ");
                Iterator i = .keySet().iterator();
                while (i.hasNext()) {
                    String k = (Stringi.next();
                    message.append(k);
                    if (i.hasNext()) {
                        message.append(' ');
                    }
                }
            }
            throwRuntimeException(message.toString());
            return null;
        }
    }

    
Use createInstance(protocol,config) to create your own Syslog instance.

First, create an implementation of SyslogConfigIF, such as UdpNetSyslogConfig.

Second, configure that configuration instance.

Third, call createInstance(protocol,config) using a short & simple String for the protocol argument.

Fourth, either use the returned instance of SyslogIF, or in later code call getInstance(protocol) with the protocol chosen in the previous step.

Parameters:
protocol
config
Returns:
Returns an instance of SyslogIF.
Throws:
SyslogRuntimeException
    public static final SyslogIF createInstance(String protocolSyslogConfigIF configthrows SyslogRuntimeException {
        if (protocol == null || "".equals(protocol.trim())) {
            throwRuntimeException("Instance protocol cannot be null or empty");
            return null;
        }
        if (config == null) {
            throwRuntimeException("SyslogConfig cannot be null");
            return null;
        }
        String syslogProtocol = protocol.toLowerCase();
        SyslogIF syslog = null;
        synchronized () {
            if (.containsKey(syslogProtocol)) {
                throwRuntimeException("Syslog protocol \"" + protocol + "\" already defined");
                return null;
            }
            try {
                Class syslogClass = config.getSyslogClass();
                syslog = (SyslogIFsyslogClass.newInstance();
            } catch (ClassCastException cse) {
                if (!config.isThrowExceptionOnInitialize()) {
                    throw new SyslogRuntimeException(cse);
                } else {
                    return null;
                }
            } catch (IllegalAccessException iae) {
                if (!config.isThrowExceptionOnInitialize()) {
                    throw new SyslogRuntimeException(iae);
                } else {
                    return null;
                }
            } catch (InstantiationException ie) {
                if (!config.isThrowExceptionOnInitialize()) {
                    throw new SyslogRuntimeException(ie);
                } else {
                    return null;
                }
            }
            syslog.initialize(syslogProtocolconfig);
            .put(syslogProtocolsyslog);
        }
        return syslog;
    }

    
initialize() sets up the default TCP and UDP Syslog protocols, as well as UNIX_SYSLOG and UNIX_SOCKET (if running on a Unix-based system).
    public synchronized static final void initialize() {
        createInstance(new UDPNetSyslogConfig());
        createInstance(new TCPNetSyslogConfig());
        if (OSDetectUtility.isUnix() && SyslogUtility.isClassExists()) {
            createInstance(new UnixSyslogConfig());
            createInstance(new UnixSocketSyslogConfig());
        }
    }

    

Parameters:
protocol - Syslog protocol
Returns:
Returns whether the protocol has been previously defined.
    public static final boolean exists(String protocol) {
        if (protocol == null || "".equals(protocol.trim())) {
            return false;
        }
        return .containsKey(protocol.toLowerCase());
    }

    
shutdown() gracefully shuts down all defined Syslog protocols, which includes flushing all queues and connections and finally clearing all instances (including those initialized by default).
    public synchronized static final void shutdown() {
        Set protocols = .keySet();
        if (protocols.size() > 0) {
            Iterator i = protocols.iterator();
            SyslogUtility.sleep(.);
            while (i.hasNext()) {
                String protocol = (Stringi.next();
                SyslogIF syslog = (SyslogIF.get(protocol);
                syslog.shutdown();
            }
            .clear();
        }
    }

    
destroyInstance() gracefully shuts down the specified Syslog protocol and removes the instance from Syslog4j.

Parameters:
protocol - the Syslog protocol to destroy
Throws:
SyslogRuntimeException
    public synchronized static final void destroyInstance(String protocolthrows SyslogRuntimeException {
        if (protocol == null || "".equals(protocol.trim())) {
            return;
        }
        String _protocol = protocol.toLowerCase();
        if (.containsKey(_protocol)) {
            SyslogUtility.sleep(.);
            SyslogIF syslog = (SyslogIF.get(_protocol);
            try {
                syslog.shutdown();
            } finally {
                .remove(_protocol);
            }
        } else {
            throwRuntimeException("Cannot destroy protocol \"" + protocol + "\" instance; call shutdown instead");
            return;
        }
    }

    
destroyInstance() gracefully shuts down the specified Syslog instance and removes it from Syslog4j.

Parameters:
syslog - the Syslog instance to destroy
Throws:
SyslogRuntimeException
    public synchronized static final void destroyInstance(SyslogIF syslogthrows SyslogRuntimeException {
        if (syslog == null) {
            return;
        }
        String protocol = syslog.getProtocol().toLowerCase();
        if (.containsKey(protocol)) {
            try {
                syslog.shutdown();
            } finally {
                .remove(protocol);
            }
        } else {
            throwRuntimeException("Cannot destroy protocol \"" + protocol + "\" instance; call shutdown instead");
            return;
        }
    }
    public static void main(String[] argsthrows Exception {
        SyslogMain.main(args);
    }
New to GrepCode? Check out our FAQ X