org.productivity.java.syslog4j.Syslog Java Examples

The following examples show how to use org.productivity.java.syslog4j.Syslog. 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: AbstractLog4jSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void setUp() {
	UDPNetSyslogConfig config = new UDPNetSyslogConfig();
	
	assertTrue(config.isCacheHostAddress());
	config.setCacheHostAddress(false);
	assertFalse(config.isCacheHostAddress());
	
	assertTrue(config.isThrowExceptionOnInitialize());
	config.setThrowExceptionOnInitialize(false);
	assertFalse(config.isThrowExceptionOnInitialize());
	
	assertFalse(config.isThrowExceptionOnWrite());
	config.setThrowExceptionOnWrite(true);
	assertTrue(config.isThrowExceptionOnWrite());
	
	Syslog.createInstance("log4jUdp",config);
	
	String protocol = getServerProtocol();
	
	startServerThread(protocol);
	SyslogUtility.sleep(100);
}
 
Example #2
Source File: AbstractNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void tearDown() {
	System.out.print("Shutting down Syslog...");
	Syslog.shutdown();
	System.out.println("done.");

	SyslogUtility.sleep(100);
	
	System.out.print("Shutting down SyslogServer...");
	SyslogServer.shutdown();
	System.out.println("done.");
	
	SyslogUtility.sleep(100);

	Syslog.initialize();
	SyslogServer.initialize();
}
 
Example #3
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 #4
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 #5
Source File: SyslogParameterTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testStructuredSyslogMessage() {
	SyslogConfigIF config = new UDPNetSyslogConfig();
	config.setUseStructuredData(true);
	assertTrue(config.isUseStructuredData());
	config.setUseStructuredData(false);
	assertFalse(config.isUseStructuredData());
	
	SyslogIF syslog = Syslog.createInstance("testStructuredSyslog",config);
	
	SyslogMessageProcessorIF messageProcessor = new StructuredSyslogMessageProcessor();
	syslog.setStructuredMessageProcessor(messageProcessor);
	assertEquals(messageProcessor,syslog.getStructuredMessageProcessor());
	
	SyslogMessageIF m1 = new StructuredSyslogMessage("test1",new HashMap(),"test2");
	SyslogMessageIF m2 = new StructuredSyslogMessage("test1",new HashMap(),"test2");
	SyslogMessageIF m3 = new StructuredSyslogMessage("test3",new HashMap(),"test2");
	SyslogMessageIF m4 = new StructuredSyslogMessage("test1",new HashMap(),"test4");
	
	assertFalse(m1.equals(null));
	assertFalse(m1.equals("Wrong Class"));
	assertTrue(m1.equals(m1));
	assertTrue(m2.equals(m2));
	
	assertFalse(m1.equals(m3));
	assertFalse(m1.equals(m4));
}
 
Example #6
Source File: PooledTCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void setupPoolConfig(boolean threaded, int maxActive, int maxWait) {
	PooledTCPNetSyslogConfig config = new PooledTCPNetSyslogConfig();
	
	config.setThreaded(threaded);
	config.setThrowExceptionOnWrite(true);
	config.setThrowExceptionOnInitialize(true);
	
	if (maxWait > 0) {
		config.setMaxWait(maxWait);
	}
	
	if (maxActive > 0) {
		config.setMaxActive(maxActive);
	}
		
	Syslog.createInstance("pooledTcp",config);
}
 
Example #7
Source File: PooledSSLTCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void setupPoolConfig(boolean threaded, int maxActive, int maxWait) {
	PooledSSLTCPNetSyslogConfig config = new PooledSSLTCPNetSyslogConfig();

	config.setThreaded(threaded);
	config.setThrowExceptionOnWrite(true);
	config.setThrowExceptionOnInitialize(true);
	
	if (maxWait > 0) {
		config.setMaxWait(maxWait);
	}
	
	if (maxActive > 0) {
		config.setMaxActive(maxActive);
	}
		
	Syslog.createInstance("pooledSslTcp",config);
}
 
Example #8
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 #9
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 #10
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 #11
Source File: SplitSyslogTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected void setMessageLength(int length) {
	SyslogConfigIF config = Syslog.getInstance("split").getConfig();
	
	String localName = SyslogUtility.getLocalName();
	this.localNameLength = localName.length();
	
	config.setMaxMessageLength(21 + this.localNameLength + length);
}
 
Example #12
Source File: MaxActiveSocketsTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected int _testMaxActiveSockets(TCPNetSyslogServerConfigIF serverConfig) {
	Counter c = new Counter();
	
	serverConfig.setMaxActiveSockets(2);
	serverConfig.addEventHandler(c);
	
	SyslogServer.createThreadedInstance("tcp_maxactivesockets",serverConfig);
	
	TCPNetSyslogConfigIF config = new TCPNetSyslogConfig();
	config.setPort(8888);
		
	SyslogIF syslog1 = Syslog.createInstance("tcp_maxactivesockets1",config);
	syslog1.info("test1");
	syslog1.flush();
	SyslogUtility.sleep(200);
	
	SyslogIF syslog2 = Syslog.createInstance("tcp_maxactivesockets2",config);
	syslog2.info("test2");
	syslog2.flush();
	SyslogUtility.sleep(200);
	
	SyslogIF syslog3 = Syslog.createInstance("tcp_maxactivesockets3",config);
	syslog3.info("test3");
	syslog3.flush();
	SyslogUtility.sleep(200);

	syslog1.shutdown();
	SyslogUtility.sleep(200);

	Syslog.destroyInstance("tcp_maxactivesockets1");
	Syslog.destroyInstance("tcp_maxactivesockets2");
	Syslog.destroyInstance("tcp_maxactivesockets3");
	
	SyslogServer.destroyInstance("tcp_maxactivesockets");
	
	return c.counter;
}
 
Example #13
Source File: MaxQueueSizeTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testMaxQueueSize() {
	int catchCount = 5;
	int maxQueueSize = 5;
	int messagesToSend = 15;
	int port = 7777;
	
	FakeSyslogServer server = new FakeSyslogServer(port,catchCount);
	Thread thread = new Thread(server);
	thread.start();
	
	BackLogCounter counter = new BackLogCounter();
	
	TCPNetSyslogConfigIF syslogConfig = new TCPNetSyslogConfig();
	syslogConfig.setPort(port);
	assertEquals(syslogConfig.getMaxQueueSize(),SyslogConstants.MAX_QUEUE_SIZE_DEFAULT);
	syslogConfig.setMaxQueueSize(maxQueueSize);
	syslogConfig.addBackLogHandler(counter);
	syslogConfig.addBackLogHandler(NullSyslogBackLogHandler.INSTANCE);
	
	SyslogIF syslog = Syslog.createInstance("maxQueueSizeTest",syslogConfig);
	
	for(int i=1; i<=messagesToSend; i++) {
		syslog.log(SyslogConstants.LEVEL_INFO,"test line " + i);
	}
	
	SyslogUtility.sleep(500);
	
	server.shutdown = true;

	SyslogUtility.sleep(500);

	System.out.println("Sent Messages:       " + messagesToSend);	
	System.out.println("Received Messages:   " + server.count);
	System.out.println("Backlogged Messages: " + counter.count);
	
	assertEquals(messagesToSend,(server.count+counter.count));
}
 
Example #14
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 #15
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 #16
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 #17
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 #18
Source File: FreshConnectionIntervalTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testFreshConnectionInterval() {
	TCPNetSyslogServerConfigIF serverConfig = new TCPNetSyslogServerConfig();
	serverConfig.setPort(8888);
	
	SocketCounter counter = new SocketCounter();
	serverConfig.addEventHandler(counter);
	
	SyslogServerIF server = SyslogServer.createThreadedInstance("tcp_8888",serverConfig);

	SyslogUtility.sleep(100);

	TCPNetSyslogConfigIF config = new TCPNetSyslogConfig();
	config.setPort(8888);
	config.setFreshConnectionInterval(300);
	
	SyslogIF syslog = Syslog.createInstance("tcp_8888",config);

	for(int i=0; i<10; i++) {
		syslog.info("message " + i);
		SyslogUtility.sleep(100);
	}
	
	SyslogUtility.sleep(100);
	
	SyslogServer.destroyInstance(server);
	Syslog.destroyInstance(syslog);

	SyslogUtility.sleep(100);

	assertEquals("OpenCounter",3,counter.openCounter);
	assertEquals("EventCounter",10,counter.eventCounter);
	assertEquals("CloseCounter",3,counter.closeCounter);
}
 
Example #19
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 #20
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 #21
Source File: NonPersistentTCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void setUp() throws Exception {
	TCPNetSyslogConfig config = new TCPNetSyslogConfig();
	config.setPersistentConnection(false);
	
	Syslog.createInstance(instanceName,config);

	super.setUp();
}
 
Example #22
Source File: SplitSyslogTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void setUp() {
	if (!Syslog.exists("split")) {
		SplitSyslogConfig config = new SplitSyslogConfig();
		
		Syslog.createInstance("split", config);
	}
}
 
Example #23
Source File: SyslogServerSessionTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testTCPSession() {
	SyslogServerConfigIF serverConfig = new TCPNetSyslogServerConfig(9999);
	
	TCPSessionHandler handler1 = new TCPSessionHandler("a");
	serverConfig.addEventHandler(handler1);
	
	TCPSessionHandler handler2 = new TCPSessionHandler("b");
	serverConfig.addEventHandler(handler2);
	
	SyslogServerIF server = SyslogServer.createThreadedInstance("tcp_session",serverConfig);
	
	SyslogUtility.sleep(100);
	
	assertTrue(handler1.initialized);
	assertTrue(handler2.initialized);
	
	SyslogConfigIF config = new TCPNetSyslogConfig();
	config.setPort(9999);
	
	TCPNetSyslogServer tcpServer = (TCPNetSyslogServer) server;
	
	SyslogIF syslog1 = Syslog.createInstance("tcp_session_1",config);
	
	syslog1.info("1");
	syslog1.info("2");

	SyslogUtility.sleep(100);

	assertEquals(1,tcpServer.getSessions().size());

	SyslogIF syslog2 = Syslog.createInstance("tcp_session_2",config);
	
	syslog2.info("3");
	syslog2.info("4");

	syslog1.info("5");
	syslog1.info("6");

	syslog2.info("7");
	syslog2.info("8");

	SyslogUtility.sleep(100);
	
	assertEquals(2,tcpServer.getSessions().size());

	syslog1.shutdown();
	syslog2.shutdown();
	SyslogServer.destroyInstance("tcp_session");
	
	try {
		SyslogServer.getInstance("tcp_session");
		fail();
		
	} catch (SyslogRuntimeException sre) {
		//
	}
	
	assertEquals(4,handler1.eventCount[0]);
	assertEquals(0,handler1.eventCount[1]);
	assertEquals(4,handler1.eventCount[2]);
	assertEquals(0,handler1.eventCount[3]);
	
	assertEquals(0,handler2.eventCount[0]);
	assertEquals(4,handler2.eventCount[1]);
	assertEquals(0,handler2.eventCount[2]);
	assertEquals(4,handler2.eventCount[3]);

	assertEquals(1,handler1.closeCount[0]);
	assertEquals(0,handler1.closeCount[1]);
	assertEquals(1,handler1.closeCount[2]);
	assertEquals(0,handler1.closeCount[3]);
	
	assertEquals(0,handler2.closeCount[0]);
	assertEquals(1,handler2.closeCount[1]);
	assertEquals(0,handler2.closeCount[2]);
	assertEquals(1,handler2.closeCount[3]);

	SyslogUtility.sleep(100);

	assertTrue(handler1.destroyed);
	assertTrue(handler2.destroyed);
}
 
Example #24
Source File: BackLogTCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testSendReceive() {
	Thread t = new Thread(new ThreadStarter(2500,"tcp"));
	t.start();
	
	List backLogEvents = new ArrayList();
	
	TestBackLogHandler bh = new TestBackLogHandler(backLogEvents);
	bh.initialize();
	
	Syslog.getInstance("tcp").getConfig().addBackLogHandler(bh);
	((AbstractSyslogConfigIF) Syslog.getInstance("tcp").getConfig()).setThreaded(false);
	
	SyslogServer.getInstance("tcp").getConfig().setShutdownWait(0);
	
	_testThreadedSendReceive(1,true,true,backLogEvents);
}
 
Example #25
Source File: SSLTCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void setupSslClient() throws Exception {
	SSLTCPNetSyslogConfig config = new SSLTCPNetSyslogConfig("127.0.0.1",10514);
	SSLConfigUtil.configure(config);
	Syslog.createInstance("sslTcp",config);
}
 
Example #26
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 #27
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 #28
Source File: TCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testTimeout() {
	TCPNetSyslogServerConfigIF serverConfig = new TCPNetSyslogServerConfig();
	serverConfig.setTimeout(150);
	serverConfig.setPort(7777);
	serverConfig.addEventHandler(new TimeoutHandler());

	TCPNetSyslogConfigIF clientConfig = new TCPNetSyslogConfig();
	clientConfig.setPort(7777);
	clientConfig.setThreaded(false);

	SyslogServer.createThreadedInstance("tcp_to",serverConfig);
	SyslogUtility.sleep(250);
	
	SyslogIF syslog = Syslog.createInstance("tcp_to",clientConfig);
	SyslogUtility.sleep(250);
	
	int mark = 0;
	
	for(int i=1; i<=10; i++) {
		if (i > 5) {
			mark--;
			
		} else {
			mark++;
		}
		
		int timeout = 35 * mark + 25;
		
		syslog.info("Test " + i + " (" + timeout + "ms) Should Timeout: " + (timeout > serverConfig.getTimeout() ? "yes" : "no"));
		SyslogUtility.sleep(timeout);
	}
	
	syslog.flush();
	SyslogUtility.sleep(200);
	
	SyslogServer.destroyInstance("tcp_to");
	
	SyslogUtility.sleep(200);
	
	Syslog.destroyInstance("tcp_to");
}
 
Example #29
Source File: TCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testThreadedSendReceive() {
	Syslog.getInstance("tcp").setMessageProcessor(SyslogMessageProcessor.getDefault());
	
	super._testThreadedSendReceive(50,true,true);
}
 
Example #30
Source File: SyslogParameterTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testSyslogExists() {
	assertFalse(Syslog.exists(null));
	assertFalse(Syslog.exists(""));
	assertTrue(Syslog.exists("udp"));
	assertEquals("udp",Syslog.getInstance("udp").getProtocol());
}