Java Code Examples for org.productivity.java.syslog4j.Syslog#getInstance()

The following examples show how to use org.productivity.java.syslog4j.Syslog#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SplitSyslogTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testTruncate() {
	setMessageLength(10);
	
	SyslogIF syslog = Syslog.getInstance("split");
	syslog.getConfig().setTruncateMessage(true);
	
	syslog.info("T");
	syslog.info("Test 1234");
	syslog.info("Test 12345");
	syslog.info("Test 654321");
	syslog.info("Test 12345 For now is the time for all good men to come to the aid of their country");
	
	assertSyslog(syslog,new String[] {"T", "Test 1234", "Test 12345", "Test 65432", "Test 12345" } );
	
	syslog.flush();
}
 
Example 2
Source File: SyslogMessageModifierVerifyTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testHashVerify() {
	SyslogIF syslog = Syslog.getInstance("tcp");
	
	HashSyslogMessageModifier modifier = HashSyslogMessageModifier.createMD5();
	modifier.getConfig().setSuffix(null);
	
	String message = "md5 hash Test 1212 ABAB";
	
	String modifiedMessage = modifier.modify(syslog,0,0,message);
	System.out.println(modifiedMessage);
	
	if (!modifier.verify(message,"fqfK2PYV76Wv9yNQjLoVeg==")) {
		fail();
	}

	if (!modifier.verify(message,Base64.decode("fqfK2PYV76Wv9yNQjLoVeg=="))) {
		fail();
	}

	if (!modifier.verify(modifiedMessage)) {
		fail();
	}
	
	assertFalse(modifier.verify(null));
}
 
Example 3
Source File: SyslogMessageModifierVerifyTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testMacVerify() {
	SyslogIF syslog = Syslog.getInstance("udp");
	
	MacSyslogMessageModifier modifier = MacSyslogMessageModifier.createHmacMD5("fb7Jl0VGnzY5ehJCdeff7bSZ5Vk=");
	modifier.getConfig().setPrefix(" ");
	modifier.getConfig().setSuffix(null);
	
	String message = "hmacmd5 Test 3434 DCDC";
	
	String modifiedMessage = modifier.modify(syslog,0,0,message);
	System.out.println(modifiedMessage);
	
	if (!modifier.verify(message,"MfWJ4XhFiMlPwnFEJ401zA==")) {
		fail();
	}

	if (!modifier.verify(message,Base64.decode("MfWJ4XhFiMlPwnFEJ401zA=="))) {
		fail();
	}

	if (!modifier.verify(modifiedMessage)) {
		fail();
	}
}
 
Example 4
Source File: AbstractNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected SyslogIF getSyslog(String protocol) {
	if (!Syslog.exists(protocol)) {
		fail("Protocol \"" + protocol + "\" does not exist");
	}
	
	SyslogIF syslog = Syslog.getInstance(protocol);
	
	if (!(syslog instanceof MultipleSyslog)) {
		syslog.getConfig().setIdent(APP_ID);
	}
	
	if (!(syslog.getConfig() instanceof MultipleSyslogConfig)) {
		syslog.getConfig().setPort(TEST_PORT);
	}
	
	return syslog;
}
 
Example 5
Source File: SyslogCreateAndDestroyTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testCreateAndDestroyByProtocol() {
	UDPNetSyslogConfig config = new UDPNetSyslogConfig();
	config.setPort(999);
	
	SyslogIF syslog = Syslog.createInstance("udpToDestroy",config);

	assertEquals(SyslogMessageProcessor.getDefault(),syslog.getMessageProcessor());		
	SyslogMessageProcessorIF messageProcessor = new FakeMessageProcessor();
	syslog.setMessageProcessor(messageProcessor);
	assertEquals(messageProcessor,syslog.getMessageProcessor());	
	
	SyslogIF matchSyslog = Syslog.getInstance("udpToDestroy");
	
	assertEquals(syslog,matchSyslog);
	
	Syslog.destroyInstance("");
	Syslog.destroyInstance("udpToDestroy");
	
	try {
		Syslog.getInstance("udpToDestroy");
		fail("udpToDestroy should not exist");
		
	} catch (SyslogRuntimeException sre) {
		//
	}
}
 
Example 6
Source File: MultipleSyslog.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void log(int level, String message) {
	for(int i=0; i<this.multipleSyslogConfig.getProtocols().size(); i++) {
		String protocol = (String) this.multipleSyslogConfig.getProtocols().get(i);
		
		SyslogIF syslog = Syslog.getInstance(protocol);
		
		syslog.log(level,message);
	}
}
 
Example 7
Source File: MultipleSyslog.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void log(int level, SyslogMessageIF message) {
	for(int i=0; i<this.multipleSyslogConfig.getProtocols().size(); i++) {
		String protocol = (String) this.multipleSyslogConfig.getProtocols().get(i);
		
		SyslogIF syslog = Syslog.getInstance(protocol);
		
		syslog.log(level,message);
	}
}
 
Example 8
Source File: MultipleSyslog.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void flush() throws SyslogRuntimeException {
	for(int i=0; i<this.multipleSyslogConfig.getProtocols().size(); i++) {
		String protocol = (String) this.multipleSyslogConfig.getProtocols().get(i);
		
		SyslogIF syslog = Syslog.getInstance(protocol);
		
		syslog.flush();
	}
}
 
Example 9
Source File: MultipleSyslog.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void shutdown() throws SyslogRuntimeException {
	for(int i=0; i<this.multipleSyslogConfig.getProtocols().size(); i++) {
		String protocol = (String) this.multipleSyslogConfig.getProtocols().get(i);
		
		SyslogIF syslog = Syslog.getInstance(protocol);
		
		syslog.shutdown();
	}
}
 
Example 10
Source File: NonDefinedSyslogInstanceTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testNonDefinedSyslogInstance() {
	try {
		Syslog.getInstance("not_defined");
		fail("not_defined shouldn't exist");
		
	} catch (SyslogRuntimeException sre) {
		assertTrue(sre.getMessage().startsWith("Syslog protocol \"not_defined\" not defined; call Syslogger.createSyslogInstance(protocol,config) first or use one of the following instances: "));
	}
}
 
Example 11
Source File: SyslogParameterTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSyslog4jBackLogHandler() {
	SyslogIF udp = Syslog.getInstance("udp");
	SyslogIF tcp = Syslog.getInstance("tcp");
	
	SyslogBackLogHandlerIF syslog4j = new Syslog4jBackLogHandler("udp");
	syslog4j.initialize();
	
	syslog4j.log(tcp,SyslogConstants.LEVEL_INFO,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.log(tcp,-1,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.down(tcp,null);
	syslog4j.up(tcp);

	syslog4j = new Syslog4jBackLogHandler("udp",false);
	
	syslog4j.log(tcp,SyslogConstants.LEVEL_INFO,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.log(tcp,-1,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.down(udp,null);
	syslog4j.up(udp);

	syslog4j = new Syslog4jBackLogHandler(udp);
	
	syslog4j.log(tcp,SyslogConstants.LEVEL_INFO,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.log(tcp,-1,"Log4j BackLog Test Message - IGNORE","really");
	
	syslog4j = new Syslog4jBackLogHandler(udp,false);
	
	syslog4j.log(tcp,SyslogConstants.LEVEL_INFO,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.log(tcp,-1,"Log4j BackLog Test Message - IGNORE","really");
	
	try {
		syslog4j.log(Syslog.getInstance("udp"),SyslogConstants.LEVEL_INFO,"Log4j BackLog Test Message - IGNORE","really");
		fail();
		
	} catch (SyslogRuntimeException sre) {
		//
	}
}
 
Example 12
Source File: JobEntrySyslog.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public Result execute( Result previousResult, int nr ) {
  Result result = previousResult;
  result.setNrErrors( 1 );
  result.setResult( false );

  String servername = environmentSubstitute( getServerName() );

  if ( Utils.isEmpty( servername ) ) {
    logError( BaseMessages.getString( PKG, "JobEntrySyslog.MissingServerName" ) );
  }

  String messageString = environmentSubstitute( getMessage() );

  if ( Utils.isEmpty( messageString ) ) {
    logError( BaseMessages.getString( PKG, "JobEntrySyslog.MissingMessage" ) );
  }

  int nrPort = Const.toInt( environmentSubstitute( getPort() ), SyslogDefs.DEFAULT_PORT );

  SyslogIF syslog = null;
  try {
    String pattern = null;

    if ( isAddTimestamp() ) {
      // add timestamp to message
      pattern = environmentSubstitute( getDatePattern() );
      if ( Utils.isEmpty( pattern ) ) {
        logError( BaseMessages.getString( PKG, "JobEntrySyslog.DatePatternEmpty" ) );
        throw new KettleException( BaseMessages.getString( PKG, "JobEntrySyslog.DatePatternEmpty" ) );
      }

    }

    // Open syslog connection
    // Set a Specific Host, then Log to It
    syslog = Syslog.getInstance( "udp" );
    syslog.getConfig().setHost( servername );
    syslog.getConfig().setPort( nrPort );
    syslog.getConfig().setFacility( getFacility() );
    syslog.getConfig().setSendLocalName( false );
    syslog.getConfig().setSendLocalTimestamp( false );
    SyslogDefs.sendMessage(
      syslog, SyslogDefs.getPriority( getPriority() ), messageString, isAddTimestamp(), pattern,
      isAddHostName() );

    // message was sent
    result.setNrErrors( 0 );
    result.setResult( true );
  } catch ( Exception e ) {
    logError( BaseMessages.getString( PKG, "JobEntrySyslog.ErrorSendingMessage", e.toString() ) );
  } finally {
    if ( syslog != null ) {
      syslog.shutdown();
    }
  }

  return result;
}
 
Example 13
Source File: ActionSyslog.java    From hop with Apache License 2.0 4 votes vote down vote up
public Result execute( Result previousResult, int nr ) {
  Result result = previousResult;
  result.setNrErrors( 1 );
  result.setResult( false );

  String servername = environmentSubstitute( getServerName() );

  if ( Utils.isEmpty( servername ) ) {
    logError( BaseMessages.getString( PKG, "ActionSyslog.MissingServerName" ) );
  }

  String messageString = environmentSubstitute( getMessage() );

  if ( Utils.isEmpty( messageString ) ) {
    logError( BaseMessages.getString( PKG, "ActionSyslog.MissingMessage" ) );
  }

  int nrPort = Const.toInt( environmentSubstitute( getPort() ), SyslogDefs.DEFAULT_PORT );

  SyslogIF syslog = null;
  try {
    String pattern = null;

    if ( isAddTimestamp() ) {
      // add timestamp to message
      pattern = environmentSubstitute( getDatePattern() );
      if ( Utils.isEmpty( pattern ) ) {
        logError( BaseMessages.getString( PKG, "ActionSyslog.DatePatternEmpty" ) );
        throw new HopException( BaseMessages.getString( PKG, "ActionSyslog.DatePatternEmpty" ) );
      }

    }

    // Open syslog connection
    // Set a Specific Host, then Log to It
    syslog = Syslog.getInstance( "udp" );
    syslog.getConfig().setHost( servername );
    syslog.getConfig().setPort( nrPort );
    syslog.getConfig().setFacility( getFacility() );
    syslog.getConfig().setSendLocalName( false );
    syslog.getConfig().setSendLocalTimestamp( false );
    SyslogDefs.sendMessage(
      syslog, SyslogDefs.getPriority( getPriority() ), messageString, isAddTimestamp(), pattern,
      isAddHostName() );

    // message was sent
    result.setNrErrors( 0 );
    result.setResult( true );
  } catch ( Exception e ) {
    logError( BaseMessages.getString( PKG, "ActionSyslog.ErrorSendingMessage", e.toString() ) );
  } finally {
    if ( syslog != null ) {
      syslog.shutdown();
    }
  }

  return result;
}
 
Example 14
Source File: UnixSyslogTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testUnixSyslog() {
	if (Boolean.getBoolean(SyslogConstants.DISABLE_UNIX_PROPERTY)) {
		assertTrue(true);
		return;
	}

	SyslogIF syslog = Syslog.getInstance(SyslogConstants.UNIX_SYSLOG);
	
	syslog.getConfig().setFacility(SyslogIF.FACILITY_KERN);
	
	syslog.error(this.getClass().getName() + ": unix_syslog " + System.currentTimeMillis());
	
	syslog.flush();
	
	syslog.shutdown();
	
	AbstractSyslog abstractSyslog = (AbstractSyslog) syslog;
	
	assertNull(abstractSyslog.getWriter());
	abstractSyslog.returnWriter(null);
}
 
Example 15
Source File: UnixSocketSyslogTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testUnixSyslog() {
	if (Boolean.getBoolean(SyslogConstants.DISABLE_UNIX_PROPERTY)) {
		assertTrue(true);
		return;
	}

	SyslogIF syslog = Syslog.getInstance(SyslogConstants.UNIX_SOCKET);
	
	UnixSocketSyslogConfig config = (UnixSocketSyslogConfig) syslog.getConfig();
	
	config.setPath("/tmp/syslog4j.sock");
	config.setType(UnixSocketSyslogConfig.SOCK_STREAM);
	
	syslog.info(this.getClass().getName() + ": unix_socket " + System.currentTimeMillis());
	
	syslog.flush();
	
	SyslogUtility.sleep(1000);
}
 
Example 16
Source File: Syslog4jBackLogHandler.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Syslog4jBackLogHandler(String protocol, boolean appendReason) {
	this.syslog = Syslog.getInstance(protocol);
	this.appendReason = appendReason;
}
 
Example 17
Source File: Syslog4jBackLogHandler.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Syslog4jBackLogHandler(String protocol) {
	this.syslog = Syslog.getInstance(protocol);
}