public void activateOptions() { super.activateOptions(); if(datePattern != null && fileName != null) { now.setTime(System.currentTimeMillis()); sdf = new SimpleDateFormat(datePattern); int type = computeCheckPeriod(); printPeriodicity(type); rc.setType(type); File file = new File(fileName); scheduledFilename = fileName+sdf.format(new Date(file.lastModified())); } else { LogLog.error("Either File or DatePattern options are not set for appender [" +name+"]."); } }
/** * This method differentiates OpenraspDailyRollingFileAppender from its * super class. * * <p>Before actually logging, this method will check whether it is * time to do a rollover. If it is, it will schedule the next * rollover time and then rollover. * */ protected void subAppend(LoggingEvent event) { long n = System.currentTimeMillis(); if (n >= nextCheck) { now.setTime(n); nextCheck = rc.getNextCheckMillis(now); try { rollOver(); } catch(IOException ioe) { if (ioe instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } LogLog.error("rollOver() failed.", ioe); } } super.subAppend(event); }
/** * Drop the connection to the remote host and release the underlying * connector thread if it has been created * */ public void cleanUp() { if(stw != null) { try { stw.close(); } catch(IOException e) { if (e instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } LogLog.error("Could not close stw.", e); } stw = null; } if(connector != null) { //LogLog.debug("Interrupting the connector."); connector.interrupted = true; connector = null; // allow gc } }
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); } }
/** * When system exit,this method will be called to close resources */ public synchronized void close() { // The synchronized modifier avoids concurrent append and close operations if (this.closed) return; LogLog.debug("Closing RocketmqLog4jAppender [" + name + "]."); this.closed = true; try { ProducerInstance.removeAndClose(this.nameServerAddress, this.producerGroup); } catch (Exception e) { LogLog.error("Closing RocketmqLog4jAppender [" + name + "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage()); } // Help garbage collection producer = null; }
private List<LogMessage> attemptToSend(List<LogMessage> batch) { if (batch.isEmpty()) return batch; PutLogEventsRequest request = new PutLogEventsRequest() .withLogGroupName(groupName) .withLogStreamName(streamName) .withLogEvents(constructLogEvents(batch)); // sending is all-or-nothing with CloudWatch; we'll return the entire batch // if there's an exception try { LogStream stream = findLogStream(); request.setSequenceToken(stream.getUploadSequenceToken()); client.putLogEvents(request); return Collections.emptyList(); } catch (Exception ex) { LogLog.error("failed to send batch", ex); return batch; } }
@Override protected void append(LoggingEvent event) { if (closed) { throw new IllegalStateException("appender is closed"); } if (! ready) { initialize(); } try { internalAppend(new LogMessage(event, getLayout())); } catch (Exception ex) { LogLog.warn("unable to append event", ex); } }
/** * Closes the current writer. */ private void stopWriter() { synchronized (initializationLock) { try { if (writer == null) return; if (layout.getFooter() != null) { internalAppend(new LogMessage(System.currentTimeMillis(), layout.getFooter())); } writer.stop(); } catch (Exception ex) { LogLog.error("exception while shutting down writer", ex); } writer = null; } }
private void internalAppend(LogMessage message) { if (message == null) return; if (isMessageTooLarge(message)) { LogLog.warn("attempted to append a message > AWS batch size; ignored"); return; } rotateIfNeeded(System.currentTimeMillis()); synchronized (messageQueueLock) { if (writer == null) { LogLog.warn("appender not properly configured: writer is null"); } else { writer.addMessage(message); lastRotationCount++; } } }
/** * Attempts to use a factory method to create the service client. * * @param clientFactoryName Fully qualified name of a static factory method. * If empty or null, this function returns null (used * to handle optionally-configured factories). * @param expectedClientClass The interface fullfilled by this client. * @param rethrow If true, any reflection exceptions will be wrapped * and rethrown; if false, exceptions return null */ protected <T> T tryClientFactory(String clientFactoryName, Class<T> expectedClientClass, boolean rethrow) { if ((clientFactoryName == null) || clientFactoryName.isEmpty()) return null; try { int methodIdx = clientFactoryName.lastIndexOf('.'); if (methodIdx < 0) throw new RuntimeException("invalid AWS client factory specified: " + clientFactoryName); Class<?> factoryKlass = Class.forName(clientFactoryName.substring(0, methodIdx)); Method factoryMethod = factoryKlass.getDeclaredMethod(clientFactoryName.substring(methodIdx + 1)); T client = expectedClientClass.cast(factoryMethod.invoke(null)); factoryMethodUsed = clientFactoryName; LogLog.debug(getClass().getSimpleName() + ": created client from factory: " + clientFactoryName); return client; } catch (Exception ex) { if (rethrow) throw new RuntimeException("unable to invoke AWS client factory", ex); else return null; } }
/** * Common support code: attempts to configure client endpoint and/or region. * * @param client A constructed writer-specific service client. * @param endpoint A possibly-null endpoint specification. */ protected <T extends AmazonWebServiceClient> T tryConfigureEndpointOrRegion(T client, String endpoint) { // explicit endpoint takes precedence over region retrieved from environment if (endpoint != null) { LogLog.debug(getClass().getSimpleName() + ": configuring endpoint: " + endpoint); client.setEndpoint(endpoint); return client; } String region = System.getenv("AWS_REGION"); if (region != null) { LogLog.debug(getClass().getSimpleName() + ": configuring region: " + region); client.configureRegion(Regions.fromName(region)); return client; } return client; }
/** * Retrieves the current AWS account ID, using reflection so that we don't * have a hard reference to the STS SDK JAR (ie, if you don't want account * IDs you don't need the JAR). * <p> * Returns null if unable to determine the account ID for any reason. */ public static String retrieveAWSAccountId() { try { Class<?> stsClientKlass = Class.forName("com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClient"); Class<?> requestKlass = Class.forName("com.amazonaws.services.securitytoken.model.GetCallerIdentityRequest"); Class<?> responseKlass = Class.forName("com.amazonaws.services.securitytoken.model.GetCallerIdentityResult"); Object stsClient = stsClientKlass.newInstance(); Object request = requestKlass.newInstance(); Method requestMethod = stsClientKlass.getMethod("getCallerIdentity", requestKlass); Object response = requestMethod.invoke(stsClient, request); Method getAccountMethod = responseKlass.getMethod("getAccount"); return (String)getAccountMethod.invoke(response); } catch (Exception ex) { LogLog.warn("substitutions: unable to retrieve AWS account ID"); return null; } }
@Override protected List<LogMessage> processBatch(List<LogMessage> currentBatch) { // although we should only ever get a single message we'll process as a list List<LogMessage> failures = new ArrayList<LogMessage>(); for (LogMessage message : currentBatch) { try { PublishRequest request = new PublishRequest() .withTopicArn(topicArn) .withMessage(message.getMessage()); if (config.subject != null) { request.setSubject(config.subject); } client.publish(request); } catch (Exception ex) { LogLog.error("failed to send message", ex); failures.add(message); } } return failures; }
/** * Attempts to find the configured topicName in the list of topics for * the current account. If successful, configures the writer and returns * true. If unsucessful, attempts to create the topic and configure as * above. */ private boolean configureByName() { if (! Pattern.matches(SNSConstants.TOPIC_NAME_REGEX, config.topicName)) { return initializationFailure("invalid topic name: " + config.topicName, null); } topicArn = retrieveAllTopicsByName().get(config.topicName); if (topicArn != null) { return true; } else { LogLog.debug("creating SNS topic: " + config.topicName); CreateTopicResult response = client.createTopic(config.topicName); topicArn = response.getTopicArn(); return true; } }
/** * Activate the options set using <tt>setHosts()</tt>, <tt>setSelector</tt> * and <tt>setMaxBackoff</tt> * * @throws FlumeException * if the LoadBalancingRpcClient cannot be instantiated. */ @Override public void activateOptions() throws FlumeException { try { final Properties properties = getProperties(hosts, selector, maxBackoff, getTimeout()); rpcClient = RpcClientFactory.getInstance(properties); if (layout != null) { layout.activateOptions(); } configured = true; } catch (Exception e) { String errormsg = "RPC client creation failed! " + e.getMessage(); LogLog.error(errormsg); if (getUnsafeMode()) { return; } throw new FlumeException(e); } }
/** * Closes underlying client. * If <tt>append()</tt> is called after this function is called, * it will throw an exception. * @throws FlumeException if errors occur during close */ @Override public synchronized void close() throws FlumeException { // Any append calls after this will result in an Exception. if (rpcClient != null) { try { rpcClient.close(); } catch (FlumeException ex) { LogLog.error("Error while trying to close RpcClient.", ex); if (unsafeMode) { return; } throw ex; } finally { rpcClient = null; } } else { String errorMsg = "Flume log4jappender already closed!"; LogLog.error(errorMsg); if (unsafeMode) { return; } throw new FlumeException(errorMsg); } }
/** * Activate the options set using <tt>setPort()</tt> * and <tt>setHostname()</tt> * * @throws FlumeException if the <tt>hostname</tt> and * <tt>port</tt> combination is invalid. */ @Override public void activateOptions() throws FlumeException { Properties props = new Properties(); props.setProperty(RpcClientConfigurationConstants.CONFIG_HOSTS, "h1"); props.setProperty(RpcClientConfigurationConstants.CONFIG_HOSTS_PREFIX + "h1", hostname + ":" + port); props.setProperty(RpcClientConfigurationConstants.CONFIG_CONNECT_TIMEOUT, String.valueOf(timeout)); props.setProperty(RpcClientConfigurationConstants.CONFIG_REQUEST_TIMEOUT, String.valueOf(timeout)); try { rpcClient = RpcClientFactory.getInstance(props); if (layout != null) { layout.activateOptions(); } } catch (FlumeException e) { String errormsg = "RPC client creation failed! " + e.getMessage(); LogLog.error(errormsg); if (unsafeMode) { return; } throw e; } }
@Override protected void subAppend(LoggingEvent event) { LogLog.debug("subAppend"); long now = System.currentTimeMillis(); if( now >= nextRollTime ) { LogLog.debug("Have to roll directory"); calculateRollOverTime(); rollDirectory(); } else if( getFile() != null && ((CountingQuietWriter) qw).getCount() >= maxFileSize ) { LogLog.debug("Have to roll file"); rollFile(); } LogLog.debug("Calling Super Sub Append"); super.subAppend(event); }
private void calculateRollOverTime() { Calendar c = Calendar.getInstance(); datedDir = new File(directory, DIRECTORY_NAME.format(c.getTime())); boolean madeDirs = datedDir.mkdirs(); if( !(madeDirs || datedDir.exists()) ) { LogLog.warn("Could not create/confirm directory " + datedDir.getAbsolutePath()); } c.add(5, 1); c.set(10, 0); c.set(12, 0); c.set(13, 0); c.set(9, 0); nextRollTime = c.getTimeInMillis(); }
/** Overwrites the jlogger field on the base class with a JDBCLoggerWithAttachment instance if: * <ul> * <li> Attachment settings are provided in the configuration file * <li> The usePreparedStatements should be true * <li> The stored-procedure, sql and sqlHandler should not be specified. * </ul> * Finally it invokes the configure() method in the base class. * @return true if the configuration was successful. */ protected boolean configure() { try { if (!((Boolean) c_configured.get(this)).booleanValue()) { // Customize the logger if attachmentTable and attachmentMDCKey are provided and if using prepared statements. // Ensure that stored-procedure, sql or sqlHandler are not being used. if (getAttachmentTable() != null && getAttachmentMDCKey() != null && isUsePreparedStatements() && getProcedure() == null && getSql() == null) { // Use reflection to check the 'sqlHandler' field from the base class, since there is no getter if (c_sqlHandler.get(this) == null) { // Use reflection to set the 'jlogger' field on the base class, since there is no setter c_jlogger.set(this, new JDBCLoggerWithAttachment(getAttachmentTable(), getAttachmentMDCKey(), getEngine())); LogLog.debug("JDBCAppenderWithAttachment::configure(), Using JDBCLoggerWithAttachment"); } } return super.configure(); } else return true; } catch (Exception e) { String errorMsg = "JDBCAppenderWithAttachment::configure()"; LogLog.error(errorMsg, e); errorHandler.error(errorMsg, e, 0); return false; } }
/** * Specify your own JDBCSqlHandler to let him create dynamic sql-statements. * * @param value * The new Sqlhandler value */ public void setSqlhandler(String value) { if (value == null) { return; } value = value.trim(); if (value.length() == 0) { return; } try { sqlHandler = (JDBCSqlHandler) (Class.forName(value).newInstance()); } catch (Exception e) { String errorMsg = "JDBCAppender::setSqlhandler(), sqlhandler must be derived of JDBCSqlHandler !"; LogLog.error(errorMsg); errorHandler.error(errorMsg, null, 0); return; } }
/** * Defines how many messages will be buffered until they will be updated to * the database. * * @param value * The new Buffer value */ public void setBuffer(String value) { if (value == null) { return; } value = value.trim(); if (value.length() == 0) { return; } try { buffer_size = Integer.parseInt(value); } catch (Exception e) { String errorMsg = "JDBCAppender::setBuffer(), Invalid BUFFER_OPTION value : " + value + " !"; LogLog.error(errorMsg); errorHandler.error(errorMsg, null, 0); return; } }
/** * 向kafka send * @param value */ private void send(String value) { // 对value的大小进行判定,当大于某个值认为该日志太大直接丢弃(防止影响到kafka) if (value.length() > 10000) { return; } final ProducerRecord<byte[], String> record = new ProducerRecord<>(this.topic, this.key, value); LazySingletonProducer.getInstance(this.config).send(record, new Callback() { @Override public void onCompletion(RecordMetadata recordMetadata, Exception e) { // TODO: 异常发生如何处理(直接停掉appender) if (null != e) { closed = true; LogLog.error("kafka send error in appender", e); // 发生异常,kafkaAppender 停止收集,向节点写入数据(监控系统会感知进行报警) if (flag.get() == true) { KafkaAppender.this.heartbeatStart(); zkRegister.write(Constants.SLASH + app + Constants.SLASH + host, NodeMode.EPHEMERAL, String.valueOf(Constants.APP_APPENDER_STOP_KEY + Constants.SEMICOLON + System.currentTimeMillis()) + Constants.SEMICOLON + SysUtil.userDir); flag.compareAndSet(true, false); } } } }); }
public void activateOptions() { super.activateOptions(); if (datePattern != null && fileName != null) { now.setTime(System.currentTimeMillis()); sdf = new SimpleDateFormat(datePattern); int type = computeCheckPeriod(); printPeriodicity(type); rc.setType(type); File file = new File(fileName); scheduledFilename = fileName + sdf.format(new Date(file.lastModified())); } else { LogLog.error("Either File or DatePattern options are not set for appender [" + name + "]."); } }
/** * This method differentiates DailyRollingFileAppender from its super class. * * <p> * Before actually logging, this method will check whether it is time to do * a rollover. If it is, it will schedule the next rollover time and then * rollover. * * @param event * the event */ protected void subAppend(LoggingEvent event) { long n = System.currentTimeMillis(); if (n >= nextCheck) { now.setTime(n); nextCheck = rc.getNextCheckMillis(now); try { rollOver(); } catch (IOException ioe) { if (ioe instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } LogLog.error("rollOver() failed.", ioe); } } super.subAppend(event); }
private JiraClientContainer getClientContainer() throws MalformedURLException, XmlRpcException { final JiraClientContainer clientContainer = new JiraClientContainer(); LogLog.debug(SIMPLE_NAME + ": Connecting to xml-rpc host on " + url); final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); config.setServerURL(new URL(url + "/rpc/xmlrpc")); clientContainer.client = new XmlRpcClient(); clientContainer.client.setConfig(config); final List params = new ArrayList(); params.add(username); params.add(password); LogLog.debug(SIMPLE_NAME + ": Attempting to login to JIRA installation at " + url + " as " + username); clientContainer.token = (String) clientContainer.client.execute("jira1.login", params); return clientContainer; }
/** * When system exit,this method will be called to close resources */ public synchronized void close() { // The synchronized modifier avoids concurrent append and close operations if (this.closed) return; LogLog.debug("Closing RocketmqLog4jAppender [" + name + "]."); this.closed = true; try { ProducerInstance.getProducerInstance().removeAndClose(this.nameServerAddress, this.producerGroup); } catch (Exception e) { LogLog.error("Closing RocketmqLog4jAppender [" + name + "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage()); } // Help garbage collection producer = null; }
/** * 关闭appender,即关闭其中所有的writer */ @Override public synchronized void close() { if (this.closed) return; this.closed = true; for (Map.Entry<String, CountingQuietWriterEx> en : writers.entrySet()) { try { en.getValue().close(); } catch (IOException e) { LogLog.error("关闭日志文件Writer失败:" + en.getKey(), e); } } writers.clear(); executor.shutdown(); }
@Override protected void append(LoggingEvent event) { String group = (String) event.getMDC(GROUP_KEY); if ("".equals(group)) group = DEFALUT_GROUP_NAME; if (!checkEntryConditions()) return; try { CountingQuietWriterEx qw = writers.get(group); if (qw == null) { qw = addCQWriter(group, getMaxLogBlockIndex(group), true); } if (qw != null) subAppend(qw, event); } catch (IOException e) { LogLog.error("写日志文件失败:" + group, e); } }
public ZeroConfSupport(String zone, int port, String name, Map properties) { //if version 3 is available, use it to constuct a serviceInfo instance, otherwise support the version1 API boolean isVersion3 = false; try { //create method is in version 3, not version 1 jmDNSClass.getMethod("create", null); isVersion3 = true; } catch (NoSuchMethodException e) { //no-op } if (isVersion3) { LogLog.debug("using JmDNS version 3 to construct serviceInfo instance"); serviceInfo = buildServiceInfoVersion3(zone, port, name, properties); } else { LogLog.debug("using JmDNS version 1.0 to construct serviceInfo instance"); serviceInfo = buildServiceInfoVersion1(zone, port, name, properties); } }
public void configure() { final Logger root = Logger.getRootLogger(); if (isResetConfiguration()) { LogManager.getLoggerRepository().resetConfiguration(); } LogLog.setInternalDebugging(isInternalDebugging()); if (isUseFileAppender()) { configureFileAppender(); } if (isUseLogCatAppender()) { configureLogCatAppender(); } root.setLevel(getRootLevel()); }
protected void checkAndConfigure() { boolean fileExists; try { fileExists = file.exists(); } catch(SecurityException e) { LogLog.warn("Was not allowed to read check file existance, file:["+ filename+"]."); interrupted = true; // there is no point in continuing return; } if(fileExists) { long l = file.lastModified(); // this can also throw a SecurityException if(l > lastModif) { // however, if we reached this point this lastModif = l; // is very unlikely. doOnChange(); warnedAlready = false; } } else { if(!warnedAlready) { LogLog.debug("["+filename+"] does not exist."); warnedAlready = true; } } }
/** Read configuration options from url <code>configURL</code>. */ public void doConfigure(java.net.URL configURL, LoggerRepository hierarchy) { Properties props = new Properties(); LogLog.debug("Reading configuration from URL " + configURL); InputStream istream = null; try { istream = configURL.openStream(); props.load(istream); } catch (Exception e) { LogLog.error("Could not read configuration file from URL [" + configURL + "].", e); LogLog.error("Ignoring configuration file [" + configURL +"]."); return; } finally { if (istream != null) { try { istream.close(); } catch(Exception ignore) { } } } doConfigure(props, hierarchy); }
/** * This method performs threshold checks and invokes filters before * delegating actual logging to the subclasses specific {@link * AppenderSkeleton#append} method. * */ public synchronized void doAppend(LoggingEvent event) { if(closed) { LogLog.error("Attempted to append to closed appender named ["+name+"]."); return; } if(!isAsSevereAsThreshold(event.getLevel())) { return; } Filter f = this.headFilter; FILTER_LOOP: while(f != null) { switch(f.decide(event)) { case Filter.DENY: return; case Filter.ACCEPT: break FILTER_LOOP; case Filter.NEUTRAL: f = f.getNext(); } } this.append(event); }
void printPeriodicity(int type) { switch (type) { case TOP_OF_MINUTE: LogLog.debug("Appender [[+name+]] to be rolled every minute."); break; case TOP_OF_HOUR: LogLog.debug("Appender [" + name + "] to be rolled on top of every hour."); break; case HALF_DAY: LogLog.debug("Appender [" + name + "] to be rolled at midday and midnight."); break; case TOP_OF_DAY: LogLog.debug("Appender [" + name + "] to be rolled at midnight."); break; case TOP_OF_WEEK: LogLog.debug("Appender [" + name + "] to be rolled at start of week."); break; case TOP_OF_MONTH: LogLog.debug("Appender [" + name + "] to be rolled at start of every month."); break; default: LogLog.warn("Unknown periodicity for appender [[+name+]]."); } }
public void run() { try { String line = dis.readUTF(); LogLog.debug("Got external roll over signal."); if(ExternallyRolledFileAppender.ROLL_OVER.equals(line)) { synchronized(er) { er.rollOver(); } dos.writeUTF(ExternallyRolledFileAppender.OK); } else { dos.writeUTF("Expecting [RollOver] string."); } dos.close(); } catch(Exception e) { LogLog.error("Unexpected exception. Exiting HUPNode.", e); } }
/** Prints the message and the stack trace of the exception on <code>System.err</code>. */ public void error(String message, Exception e, int errorCode, LoggingEvent event) { if (e instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } LogLog.debug("FB: The following error reported: " + message, e); LogLog.debug("FB: INITIATING FALLBACK PROCEDURE."); if (loggers != null) { for(int i = 0; i < loggers.size(); i++) { Logger l = (Logger) loggers.elementAt(i); LogLog.debug("FB: Searching for ["+primary.getName()+"] in logger [" +l.getName() + "]."); LogLog.debug("FB: Replacing ["+primary.getName()+"] by [" + backup.getName() + "] in logger ["+ l.getName() +"]."); l.removeAppender(primary); LogLog.debug("FB: Adding appender ["+backup.getName()+"] to logger " + l.getName()); l.addAppender(backup); } } }
/** Activate the specified options, such as the smtp host, the recipient, from, etc. */ public void activateOptions() { Session session = createSession(); msg = new MimeMessage(session); try { addressMessage(msg); if(subject != null) { try { msg.setSubject(MimeUtility.encodeText(subject, "UTF-8", null)); } catch(UnsupportedEncodingException ex) { LogLog.error("Unable to encode SMTP subject", ex); } } } catch(MessagingException e) { LogLog.error("Could not activate SMTPAppender options.", e ); } if (evaluator instanceof OptionHandler) { ((OptionHandler) evaluator).activateOptions(); } }