org.productivity.java.syslog4j.SyslogIF Java Examples

The following examples show how to use org.productivity.java.syslog4j.SyslogIF. 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: 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 #2
Source File: MacSyslogMessageModifier.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String modify(SyslogIF syslog, int facility, int level, String message) {
	synchronized(this.mac) {
		byte[] messageBytes = SyslogUtility.getBytes(syslog.getConfig(),message);
		
		StringBuffer buffer = new StringBuffer(message);
		
		byte[] macBytes = this.mac.doFinal(messageBytes);
		
		String macString = Base64.encodeBytes(macBytes,Base64.DONT_BREAK_LINES);
		
		buffer.append(this.config.getPrefix());
		buffer.append(macString);
		buffer.append(this.config.getSuffix());
		
		return buffer.toString();
	}
}
 
Example #3
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 #4
Source File: UDPPCISyslogMessageTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testPCISyslogMessage() {
	// PREPARE
	
	List events = new ArrayList();
	String message = null;
	
	String protocol = getClientProtocol();
	SyslogIF syslog = getSyslog(protocol);
	
	message = new PCISyslogMessage("a","b",new Date(),"c","d","e").createMessage();
	syslog.info("[TEST] " + message);
	events.add("[TEST] " + message);

	// VERIFY
	
	SyslogUtility.sleep(pause);
	syslog.flush();
	verifySendReceive(events,false,false);
}
 
Example #5
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 #6
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 #7
Source File: ChecksumSyslogMessageModifier.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String modify(SyslogIF syslog, int facility, int level, String message) {
	synchronized(this.config.getChecksum()) {
		StringBuffer messageBuffer = new StringBuffer(message);
		
		byte[] messageBytes = SyslogUtility.getBytes(syslog.getConfig(),message);
		
		if (!this.config.isContinuous()) {
			this.config.getChecksum().reset();
		}
		
		this.config.getChecksum().update(messageBytes,0,message.length());
		
		messageBuffer.append(this.config.getPrefix());
		messageBuffer.append(Long.toHexString(this.config.getChecksum().getValue()).toUpperCase());
		messageBuffer.append(this.config.getSuffix());
		
		return messageBuffer.toString();
	}
}
 
Example #8
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 #9
Source File: Syslog4jAppender.java    From logback-syslog4j with MIT License 6 votes vote down vote up
@Override
public void start() {
    super.start();

    synchronized (this) {
        try {
            Class syslogClass = syslogConfig.getSyslogClass();
            syslog = (SyslogIF) syslogClass.newInstance();

            syslog.initialize(syslogClass.getSimpleName(), syslogConfig);
        } catch (ClassCastException cse) {
            throw new SyslogRuntimeException(cse);
        } catch (IllegalAccessException iae) {
            throw new SyslogRuntimeException(iae);
        } catch (InstantiationException ie) {
            throw new SyslogRuntimeException(ie);
        }
    }
}
 
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 6 votes vote down vote up
protected void assertSyslog(SyslogIF syslog, String[] expectedMessages) {
	SplitSyslog splitSyslog = (SplitSyslog) syslog;
	
	List lastMessages = splitSyslog.getLastMessages();
	
	if (lastMessages.size() < 1) {
		fail("No messages received");
	}
	
	for(int i=0; i<lastMessages.size(); i++) {
		String lastMessage = ((String) lastMessages.get(i)).substring(21 + this.localNameLength);
		
		assertEquals(expectedMessages[i],lastMessage);
	}
	
	syslog.flush();
}
 
Example #12
Source File: Syslog4jAppender.java    From logback-syslog4j with MIT License 5 votes vote down vote up
/**
 * Convert a level to equivalent syslog severity. Only levels for printing
 * methods i.e DEBUG, WARN, INFO and ERROR are converted.
 *
 * @see ch.qos.logback.core.net.SyslogAppenderBase#getSeverityForEvent(java.lang.Object)
 */
public int getSeverityForEvent(Object eventObject) {
    if (eventObject instanceof ILoggingEvent) {
        ILoggingEvent event = (ILoggingEvent) eventObject;
        return LevelToSyslogSeverity.convert(event);
    } else {
        return SyslogIF.LEVEL_INFO;
    }
}
 
Example #13
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 #14
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 #15
Source File: HTMLEntityEscapeSyslogMessageModifier.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String modify(SyslogIF syslog, int facility, int level, String message) {
	if (message != null && !"".equals(message.trim())) {
		String escapedMessage = escapeHtml(message);
		
		return escapedMessage;
	}
	
	return message;
}
 
Example #16
Source File: PrintStreamSyslogBackLogHandler.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void log(SyslogIF syslog, int level, String message, String reason) {
	String combinedMessage = combine(syslog,level,message,reason);
	
	if (this.appendLinefeed) {
		this.printStream.println(combinedMessage);
		
	} else {
		this.printStream.print(combinedMessage);
	}
}
 
Example #17
Source File: Syslog4jBackLogHandler.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void log(SyslogIF syslog, int level, String message, String reason) throws SyslogRuntimeException {
	if (this.syslog.getProtocol().equals(syslog.getProtocol())) {
		throw new SyslogRuntimeException("Ignoring this log entry since the backLog protocol \"" + this.syslog.getProtocol() + "\" is the same as the main protocol");
	}
	
	String combinedMessage = combine(syslog,level,message,reason);
	
	this.syslog.log(level,combinedMessage);
}
 
Example #18
Source File: SuffixSyslogMessageModifier.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String modify(SyslogIF syslog, int facility, int level, String message) {
	if (this.suffix == null || "".equals(this.suffix.trim())) {
		return message;
	}

	return message + this.delimiter + this.suffix;
}
 
Example #19
Source File: PrefixSyslogMessageModifier.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String modify(SyslogIF syslog, int facility, int level, String message) {
	if (this.prefix == null || "".equals(this.prefix.trim())) {
		return message;
	}

	return this.prefix + this.delimiter + message;
}
 
Example #20
Source File: StringCaseSyslogMessageModifier.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String modify(SyslogIF syslog, int facility, int level, String message) {
	String _message = message;
	
	if (message != null) {
		if (this.stringCase == LOWER_CASE) {
			_message = _message.toLowerCase();
			
		} else if (this.stringCase == UPPER_CASE) {
			_message = _message.toUpperCase();
		}
	}
	
	return _message;
}
 
Example #21
Source File: SequentialSyslogMessageModifier.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String modify(SyslogIF syslog, int facility, int level, String message) {
	StringBuffer buffer = new StringBuffer(message);
	
	buffer.append(this.config.getPrefix());
	buffer.append(nextSequence(level));
	buffer.append(this.config.getSuffix());
	
	return buffer.toString();
}
 
Example #22
Source File: AbstractSyslogBackLogHandler.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected String combine(SyslogIF syslog, int level, String message, String reason) {
	// Note: syslog is explicitly ignored by default
	
	String _message = message != null ? message : "UNKNOWN";
	String _reason = reason != null ? reason : "UNKNOWN";
	
	String combinedMessage = SyslogUtility.getLevelString(level) + " " + _message;
	
	if (this.appendReason) {
		combinedMessage += " [" + _reason + "]";
	}
	
	return combinedMessage;
}
 
Example #23
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 #24
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 #25
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 #26
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 #27
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 #28
Source File: SyslogMessageModifierTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testHTMLEntityEscapeMessageModifier() {
	SyslogMessageModifierIF messageModifier = new HTMLEntityEscapeSyslogMessageModifier();

	String nullHtml = messageModifier.modify(null,0,0,null);
	assertNull(nullHtml);
	
	String emptyHtml = messageModifier.modify(null,0,0,"");
	assertEquals("",emptyHtml);

	// PREPARE
	
	List events = new ArrayList();
	String message = null;
	
	String protocol = getClientProtocol();
	SyslogIF syslog = getSyslog(protocol);
	syslog.getConfig().removeAllMessageModifiers();		

	syslog.getConfig().addMessageModifier(HTMLEntityEscapeSyslogMessageModifier.createDefault());
	
	// SEND HTML

	message = "[TEST] <html>&\"test\"&'" + "\t" + "</html>" + (char) 255;
	syslog.info(message);
	events.add("[TEST] &lt;html&gt;&amp;&quot;test&quot;&amp;&#39;&#9;&lt;/html&gt;&#255;");

	// VERIFY
	
	SyslogUtility.sleep(pause);
	syslog.flush();
	verifySendReceive(events,false,false);
}
 
Example #29
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 #30
Source File: AbstractNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public ClientThread(SyslogIF syslog, List messages) {
	this.syslog = syslog;
	this.messages = messages;
}