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+"].");
}
}
java类org.apache.log4j.helpers.LogLog的实例源码
OpenraspDailyRollingFileAppender.java 文件源码
项目:openrasp
阅读 22
收藏 0
点赞 0
评论 0
OpenraspDailyRollingFileAppender.java 文件源码
项目:openrasp
阅读 21
收藏 0
点赞 0
评论 0
/**
* 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);
}
SyslogTcpAppender.java 文件源码
项目:openrasp
阅读 20
收藏 0
点赞 0
评论 0
/**
* 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
}
}
SyslogTcpAppender.java 文件源码
项目:openrasp
阅读 29
收藏 0
点赞 0
评论 0
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);
}
}
RocketmqLog4jAppender.java 文件源码
项目:rocketmq-rocketmq-all-4.1.0-incubating
阅读 20
收藏 0
点赞 0
评论 0
/**
* 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;
}
CloudWatchLogWriter.java 文件源码
项目:log4j-aws-appenders
阅读 22
收藏 0
点赞 0
评论 0
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;
}
}
AbstractAppender.java 文件源码
项目:log4j-aws-appenders
阅读 20
收藏 0
点赞 0
评论 0
@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);
}
}
AbstractAppender.java 文件源码
项目:log4j-aws-appenders
阅读 24
收藏 0
点赞 0
评论 0
/**
* 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;
}
}
AbstractAppender.java 文件源码
项目:log4j-aws-appenders
阅读 26
收藏 0
点赞 0
评论 0
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++;
}
}
}
AbstractLogWriter.java 文件源码
项目:log4j-aws-appenders
阅读 18
收藏 0
点赞 0
评论 0
/**
* 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;
}
}
AbstractLogWriter.java 文件源码
项目:log4j-aws-appenders
阅读 19
收藏 0
点赞 0
评论 0
/**
* 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;
}
Utils.java 文件源码
项目:log4j-aws-appenders
阅读 31
收藏 0
点赞 0
评论 0
/**
* 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;
}
}
SNSLogWriter.java 文件源码
项目:log4j-aws-appenders
阅读 29
收藏 0
点赞 0
评论 0
@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;
}
SNSLogWriter.java 文件源码
项目:log4j-aws-appenders
阅读 20
收藏 0
点赞 0
评论 0
/**
* 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;
}
}
LoadBalancingLog4jAppender.java 文件源码
项目:flume-release-1.7.0
阅读 22
收藏 0
点赞 0
评论 0
/**
* 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);
}
}
Log4jAppender.java 文件源码
项目:flume-release-1.7.0
阅读 23
收藏 0
点赞 0
评论 0
/**
* 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);
}
}
Log4jAppender.java 文件源码
项目:flume-release-1.7.0
阅读 25
收藏 0
点赞 0
评论 0
/**
* 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;
}
}
DailySizeRollingAppender.java 文件源码
项目:Equella
阅读 23
收藏 0
点赞 0
评论 0
@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);
}
DailySizeRollingAppender.java 文件源码
项目:Equella
阅读 27
收藏 0
点赞 0
评论 0
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();
}
DailySizeRollingAppender.java 文件源码
项目:Equella
阅读 26
收藏 0
点赞 0
评论 0
@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);
}
DailySizeRollingAppender.java 文件源码
项目:Equella
阅读 24
收藏 0
点赞 0
评论 0
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();
}
JDBCAppenderWithAttachment.java 文件源码
项目:jaffa-framework
阅读 28
收藏 0
点赞 0
评论 0
/** 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;
}
}
JDBCAppender.java 文件源码
项目:jaffa-framework
阅读 21
收藏 0
点赞 0
评论 0
/**
* 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;
}
}
JDBCAppender.java 文件源码
项目:jaffa-framework
阅读 24
收藏 0
点赞 0
评论 0
/**
* 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;
}
}
KafkaAppender.java 文件源码
项目:SkyEye
阅读 24
收藏 0
点赞 0
评论 0
/**
* 向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);
}
}
}
});
}
BackupEnabledDailyRollingFileAppender.java 文件源码
项目:utils4j
阅读 20
收藏 0
点赞 0
评论 0
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 + "].");
}
}
BackupEnabledDailyRollingFileAppender.java 文件源码
项目:utils4j
阅读 19
收藏 0
点赞 0
评论 0
/**
* 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);
}
JIRALog4jAppender.java 文件源码
项目:log4j-appender-jira
阅读 19
收藏 0
点赞 0
评论 0
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;
}
RocketmqLog4jAppender.java 文件源码
项目:rocketmq
阅读 18
收藏 0
点赞 0
评论 0
/**
* 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;
}
GroupRollingFileAppender.java 文件源码
项目:jcode
阅读 19
收藏 0
点赞 0
评论 0
/**
* 关闭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();
}