void connect(InetAddress address, int port) { if(this.address == null) return; try { // First, close the previous connection if any. cleanUp(); stw = new SyslogTcpWriter(new Socket(address, port).getOutputStream(), syslogFacility); } catch(IOException e) { if (e instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } String msg = "Could not connect to remote log4j server at [" +address.getHostName()+"]."; if(reconnectionDelay > 0) { msg += " We will try again later."; fireConnector(); // fire the connector thread } else { msg += " We are not retrying."; errorHandler.error(msg, e, ErrorCode.GENERIC_FAILURE); } LogLog.error(msg); } }
@Override protected void append(LoggingEvent event) { if (this.layout == null) { errorHandler.error("No layout for appender " + name, null, ErrorCode.MISSING_LAYOUT); return; } String message = this.layout.format(event); Session session = sessions.get(event.getThreadName()); if (session != null) { try { session.getBasicRemote().sendText(gson.toJson(new Response(message, "", false))); } catch (IOException ex) { System.err.println("IOException " + ex.getMessage()); } } else { System.err.println("No session found for thread " + event.getThreadName()); } }
/** This method called by {@link AppenderSkeleton#doAppend} method to do most of the real appending work. */ public void append(LoggingEvent event) { if(!checkEntryConditions()) { return; } try { ObjectMessage msg = topicSession.createObjectMessage(); if(locationInfo) { event.getLocationInformation(); } msg.setObject(event); topicPublisher.publish(msg); } catch(Exception e) { errorHandler.error("Could not publish message in JMSAppender ["+name+"].", e, ErrorCode.GENERIC_FAILURE); } }
void connect(InetAddress address, int port) { if(this.address == null) return; try { // First, close the previous connection if any. cleanUp(); oos = new ObjectOutputStream(new Socket(address, port).getOutputStream()); } catch(IOException e) { String msg = "Could not connect to remote log4j server at [" +address.getHostName()+"]."; if(reconnectionDelay > 0) { msg += " We will try again later."; fireConnector(); // fire the connector thread } else { msg += " We are not retrying."; errorHandler.error(msg, e, ErrorCode.GENERIC_FAILURE); } LogLog.error(msg); } }
/** If the value of <b>File</b> is not <code>null</code>, then {@link #setFile} is called with the values of <b>File</b> and <b>Append</b> properties. @since 0.8.1 */ public void activateOptions() { if(fileName != null) { try { setFile(fileName, fileAppend, bufferedIO, bufferSize); } catch(java.io.IOException e) { errorHandler.error("setFile("+fileName+","+fileAppend+") call failed.", e, ErrorCode.FILE_OPEN_FAILURE); } } else { //LogLog.error("File option not set for appender ["+name+"]."); LogLog.warn("File option not set for appender ["+name+"]."); LogLog.warn("Are you using FileAppender instead of ConsoleAppender?"); } }
/** * Overriding this method to activate the options for this class * i.e. Looking up the Connection factory ... */ public void activateOptions() { QueueConnectionFactory queueConnectionFactory; try { Context ctx = getInitialContext(); queueConnectionFactory = (QueueConnectionFactory) ctx.lookup(queueConnectionFactoryBindingName); queueConnection = queueConnectionFactory.createQueueConnection(); queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = (Queue) ctx.lookup(queueBindingName); queueSender = queueSession.createSender(queue); queueConnection.start(); ctx.close(); } catch(Exception e) { errorHandler.error("Error while activating options for appender named ["+name+ "].", e, ErrorCode.GENERIC_FAILURE); } }
/** * This method called by {@link AppenderSkeleton#doAppend} method to * do most of the real appending work. The LoggingEvent will be * be wrapped in an ObjectMessage to be put on the JMS queue. */ public void append(LoggingEvent event) { if(!checkEntryConditions()) { return; } try { ObjectMessage msg = queueSession.createObjectMessage(); msg.setObject(event); queueSender.send(msg); } catch(Exception e) { errorHandler.error("Could not send message in JMSQueueAppender ["+name+"].", e, ErrorCode.GENERIC_FAILURE); } }
InternetAddress[] parseAddress(String addressStr) { try { InternetAddress[] as = InternetAddress.parse(addressStr, true); if (as != null && as.length > 0) { for (InternetAddress a : as) { a.setPersonal(a.getPersonal()); } } return as; }catch (UnsupportedEncodingException e1){ errorHandler.error("Could not parse address ["+addressStr+"].", e1, ErrorCode.ADDRESS_PARSE_FAILURE); } catch(AddressException e) { errorHandler.error("Could not parse address ["+addressStr+"].", e, ErrorCode.ADDRESS_PARSE_FAILURE); } return null; }
/** * loops through the buffer of LoggingEvents, gets a * sql string from getLogStatement() and sends it to execute(). * Errors are sent to the errorHandler. * * If a statement fails the LoggingEvent stays in the buffer! */ public void flushBuffer() { //Do the actual logging removes.ensureCapacity(buffer.size()); for (Iterator i = buffer.iterator(); i.hasNext();) { LoggingEvent logEvent = (LoggingEvent)i.next(); try { String sql = getLogStatement(logEvent); execute(sql); } catch (SQLException e) { errorHandler.error("Failed to excute sql", e, ErrorCode.FLUSH_FAILURE); } finally { removes.add(logEvent); } } // remove from the buffer any events that were reported buffer.removeAll(removes); // clear the buffer of reported events removes.clear(); }
void connect(InetAddress address, int port) { if(this.address == null) return; try { // First, close the previous connection if any. cleanUp(); oos = new ObjectOutputStream(new Socket(address, port).getOutputStream()); } catch(IOException e) { if (e instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } String msg = "Could not connect to remote log4j server at [" +address.getHostName()+"]."; if(reconnectionDelay > 0) { msg += " We will try again later."; fireConnector(); // fire the connector thread } else { msg += " We are not retrying."; errorHandler.error(msg, e, ErrorCode.GENERIC_FAILURE); } LogLog.error(msg); } }
/** * @see org.apache.log4j.AppenderSkeleton#activateOptions() */ @Override public void activateOptions() { try { // Close previous connections if reactivating if (mongo != null) { close(); } initialize(); } catch (final Exception e) { errorHandler.error("Unexpected exception while initialising MongoDbAppender.", e, ErrorCode.GENERIC_FAILURE); } }
private List<Integer> getPortNums(final String[] ports) { final List<Integer> portNums = new ArrayList<Integer>(); for (final String port : ports) { try { final Integer portNum = Integer.valueOf(port.trim()); if (portNum < 0) { errorHandler.error( "MongoDB appender port property can't contain a negative integer", null, ErrorCode.ADDRESS_PARSE_FAILURE); } else { portNums.add(portNum); } } catch (final NumberFormatException e) { errorHandler.error( "MongoDB appender can't parse a port property value into an integer", e, ErrorCode.ADDRESS_PARSE_FAILURE); } } return portNums; }
/** * Method that creates query for OrientDB from the JSONObject event in the queue * and executes it in the database * @see {@link DataBaseAppender}{@link #saveQueue()} */ @Override protected void saveQueue() { // TODO Auto-generated method stub if (queue.size() == 0) return; StringBuffer cmd = new StringBuffer("begin\n"); for (Object obj : queue) { cmd.append("insert into " + table + " content " + ((JSONObject) obj).toString() + "\n"); } cmd.append("commit\n"); //System.out.println(cmd); try { database.command(new OCommandScript("sql", cmd.toString())).execute(); } catch(Exception e){ errorHandler.error("Unexpected exception while saving events", e, ErrorCode.GENERIC_FAILURE); } }
/** * To a log event to the logger. * * @param event * LoggingEvent. */ @Override public void append( LoggingEvent event ) { try { dirName = ( String ) event.getMDC( "SettopMac" ); fileName = ( String ) event.getMDC( "LogFileName" ); String logFileName = System.getProperty( "cats.home" ) + File.separator + dirName + File.separator + fileName; setFile( logFileName, fileAppend, bufferedIO, bufferSize ); MDC.remove( "SettopMac" ); MDC.remove( "LogFileName" ); } catch ( IOException ie ) { errorHandler.error( "Error occured while setting file for the log level " + event.getLevel(), ie, ErrorCode.FILE_OPEN_FAILURE ); } super.append( event ); }
/** * Closes this appender instance. Before exiting, the implementation tries to * flush out buffered log events within configured shutdownTimeout seconds. If * that doesn't finish within configured shutdownTimeout, it would drop all * the buffered log events. */ @Override public void close() { ThreadPoolExecutor threadpool = (ThreadPoolExecutor) kinesisClient.getExecutorService(); threadpool.shutdown(); BlockingQueue<Runnable> taskQueue = threadpool.getQueue(); int bufferSizeBeforeShutdown = threadpool.getQueue().size(); boolean gracefulShutdown = true; try { gracefulShutdown = threadpool.awaitTermination(shutdownTimeout, TimeUnit.SECONDS); } catch (InterruptedException e) { // we are anyways cleaning up } finally { int bufferSizeAfterShutdown = taskQueue.size(); if (!gracefulShutdown || bufferSizeAfterShutdown > 0) { String errorMsg = "Kinesis Log4J Appender (" + name + ") waited for " + shutdownTimeout + " seconds before terminating but could send only " + (bufferSizeAfterShutdown - bufferSizeBeforeShutdown) + " logevents, it failed to send " + bufferSizeAfterShutdown + " pending log events from it's processing queue"; LOGGER.error(errorMsg); errorHandler.error(errorMsg, null, ErrorCode.WRITE_FAILURE); } } kinesisClient.shutdown(); }
/** * This method is called whenever a logging happens via logger.log(..) API * calls. Implementation for this appender will take in log events instantly * as long as the buffer is not full (as per user configuration). This call * will block if internal buffer is full until internal threads create some * space by publishing some of the records. * * If there is any error in parsing logevents, those logevents would be * dropped. */ @Override public void append(LoggingEvent logEvent) { if (initializationFailed) { error("Check the configuration and whether the configured stream " + streamName + " exists and is active. Failed to initialize kinesis log4j appender: " + name); return; } try { String message = layout.format(logEvent); ByteBuffer data = ByteBuffer.wrap(message.getBytes(encoding)); kinesisClient.putRecordAsync(new PutRecordRequest().withPartitionKey(UUID.randomUUID().toString()) .withStreamName(streamName).withData(data), asyncCallHander); } catch (Exception e) { LOGGER.error("Failed to schedule log entry for publishing into Kinesis stream: " + streamName); errorHandler.error("Failed to schedule log entry for publishing into Kinesis stream: " + streamName, e, ErrorCode.WRITE_FAILURE, logEvent); } }
/** * Called when a new log attempt is made, either due to server restart or rollover. The filename * is modified to identify the logging node in getNewFileName. */ @Override public void activateOptions() { if (fileName != null) { if (!fileName.equals(mCurrentFileName)) { mOriginalFileName = fileName; } else { fileName = mOriginalFileName; } try { fileName = getNewLogFileName(fileName); setFile(fileName, fileAppend, bufferedIO, bufferSize); } catch (Exception e) { errorHandler.error("Error while activating log options", e, ErrorCode.FILE_OPEN_FAILURE); } } }
public final void write(final String string) { if (string == null) { /* * Create an exception here so that a stack trace is available for * debugging purposes. */ errorHandler.error("Attempt to write null - see Log4J bug list.", new RuntimeException("Fault in Log4J Layout"), ErrorCode.WRITE_FAILURE); return; } try { synchronized (lock) { final int length = string.length(); out.write(string, 0, length); this.charCount += length; } } catch (IOException e) { if (e instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } errorHandler.error("Failed to write [" + string + "].", e, ErrorCode.WRITE_FAILURE); } }
public void activateOptions() { if (fileName != null) { try { Date dNow = new Date( ); SimpleDateFormat ft = new SimpleDateFormat ("yyyy_MM_dd_hh_mm_ss"); String dirName = "Log_" + ft.format(dNow); File dir = new File("log/" + dirName); dir.mkdir(); fileName = dir.getPath() + "/calculated_values.log"; setFile(fileName, fileAppend, bufferedIO, bufferSize); } catch (Exception e) { errorHandler.error("Error while activating log options", e, ErrorCode.FILE_OPEN_FAILURE); } } }
public void activateOptions() { if (fileName != null) { try { Date dNow = new Date( ); SimpleDateFormat ft = new SimpleDateFormat ("yyyy_MM_dd_hh_mm_ss"); String dirName = "Log_" + ft.format(dNow); File dir = new File("log/" + dirName); dir.mkdir(); fileName = dir.getPath() + "/telemetry.log"; setFile(fileName, fileAppend, bufferedIO, bufferSize); } catch (Exception e) { errorHandler.error("Error while activating log options", e, ErrorCode.FILE_OPEN_FAILURE); } } }
public void activateOptions() { if (fileName != null) { try { Date dNow = new Date( ); SimpleDateFormat ft = new SimpleDateFormat ("yyyy_MM_dd_hh_mm_ss"); String dirName = "Log_" + ft.format(dNow); File dir = new File("log/" + dirName); dir.mkdir(); fileName = dir.getPath() + "/device_errors.log"; setFile(fileName, fileAppend, bufferedIO, bufferSize); } catch (Exception e) { errorHandler.error("Error while activating log options", e, ErrorCode.FILE_OPEN_FAILURE); } } }
/** * @see org.apache.log4j.AppenderSkeleton#activateOptions() */ @Override public void activateOptions() { try { // Close previous connections if reactivating if (httpDataCollector != null) { //httpDataCollector.close(); close(); } if (StringUtils.isEmpty(workspaceId)) { throw new Exception( String.format("the Log4jALAAppender property workspaceId [%s] shouldn't be empty (log4j.xml)", this.workspaceId)); } if (StringUtils.isEmpty(sharedKey)) { throw new Exception(String.format( "the Log4jALAAppender property sharedKey [%s] shouldn't be empty (log4j.xml)", this.sharedKey)); } if (StringUtils.isEmpty(logType)) { throw new Exception(String.format( "the Log4jALAAppender property logType [%s] shouldn't be empty (log4j.xml)", this.logType)); } serializer = new LoggingEventSerializer(); httpDataCollector = new HTTPDataCollector(this.workspaceId, this.sharedKey, this.threadPoolSize <= 0 ? 1000 : this.threadPoolSize, this, this.proxyHost, this.proxyPort); } catch (Exception e) { errorHandler.error("Unexpected exception while initialising HTTPDataCollector.", e, ErrorCode.GENERIC_FAILURE); } }
public void close() { flushBuffer(); try { if (connection != null && !connection.isClosed()) connection.close(); } catch (SQLException e) { errorHandler.error("Error closing connection", e, ErrorCode.GENERIC_FAILURE); } this.closed = true; }
public void setDriver(String driverClass) { try { Class.forName(driverClass); } catch (Exception e) { errorHandler.error("Failed to load driver", e, ErrorCode.GENERIC_FAILURE); } }
public void write(String string) { try { out.write(string); } catch(IOException e) { errorHandler.error("Failed to write ["+string+"].", e, ErrorCode.WRITE_FAILURE); } }
public void flush() { try { out.flush(); } catch(IOException e) { errorHandler.error("Failed to flush writer,", e, ErrorCode.FLUSH_FAILURE); } }
public void write(String string) { try { out.write(string); count += string.length(); } catch(IOException e) { errorHandler.error("Write failure.", e, ErrorCode.WRITE_FAILURE); } }
InternetAddress getAddress(String addressStr) { try { return new InternetAddress(addressStr); } catch(AddressException e) { errorHandler.error("Could not parse address ["+addressStr+"].", e, ErrorCode.ADDRESS_PARSE_FAILURE); return null; } }
InternetAddress[] parseAddress(String addressStr) { try { return InternetAddress.parse(addressStr, true); } catch(AddressException e) { errorHandler.error("Could not parse address ["+addressStr+"].", e, ErrorCode.ADDRESS_PARSE_FAILURE); return null; } }
/** If the a value for {@link #FILE_OPTION} is non-null, then {@link #setFile} is called with the values of {@link #FILE_OPTION} and {@link #APPEND_OPTION}. @since 0.8.1 */ public void activateOptions() { try { setFile(null, super.fileAppend); } catch(java.io.IOException e) { errorHandler.error("setFile(null,"+fileAppend+") call failed.", e, ErrorCode.FILE_OPEN_FAILURE); } }
InternetAddress getAddress(String addressStr) { InternetAddress internetAddress = null; try { internetAddress = new InternetAddress(addressStr); internetAddress.setPersonal(internetAddress.getPersonal()); return internetAddress; }catch (UnsupportedEncodingException e1){ errorHandler.error("Could not parse address ["+addressStr+"].", e1, ErrorCode.ADDRESS_PARSE_FAILURE); } catch(AddressException e) { errorHandler.error("Could not parse address ["+addressStr+"].", e, ErrorCode.ADDRESS_PARSE_FAILURE); } return null; }
protected void addressMessage(final Message msg) throws MessagingException { if (from != null) { InternetAddress internetAddress = getAddress(from); if(this.nickName!=null) { try{ internetAddress.setPersonal(this.nickName, this.getEncoding()); }catch (UnsupportedEncodingException e1){ errorHandler.error("Could not parse address ["+internetAddress+"].", e1, ErrorCode.ADDRESS_PARSE_FAILURE); } } msg.setFrom(internetAddress); } else { msg.setFrom(); } //Add ReplyTo addresses if defined. if (replyTo != null && replyTo.length() > 0) { msg.setReplyTo(parseAddress(replyTo)); } if (to != null && to.length() > 0) { msg.setRecipients(Message.RecipientType.TO, parseAddress(to)); } //Add CC receipients if defined. if (cc != null && cc.length() > 0) { msg.setRecipients(Message.RecipientType.CC, parseAddress(cc)); } //Add BCC receipients if defined. if (bcc != null && bcc.length() > 0) { msg.setRecipients(Message.RecipientType.BCC, parseAddress(bcc)); } }