I have a bunch of classes in com.example. I want my logfile to capture debug logging and my console to capture info only.
I am using log4j2.
Here is my log4j2.properties file.
name=PropertiesConfig
property.filename = logs
# configure both the appenders
# CONSOLE APPENDER
appender.console.type = console
appender.console.name = consoleAppender
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%t] %c{1} - %msg%n
# LOGFILE appender
appender.logfile.type = File
appender.logfile.name = logfileAppender
appender.logfile.fileName=extract.log
appender.logfile.layout.type=PatternLayout
appender.logfile.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
logger.example.name = com.example
logger.example.level = debug
logger.example.additivity = true
logger.example.appenderRef.logfile.ref = logfileAppender
logger.example.appenderRef.console.ref = consoleAppender
# configure the loggers, starting from the root logger.
rootLogger.level = info
Which logs to debug at both.
I try this...
name=PropertiesConfig
property.filename = logs
# configure both the appenders
# CONSOLE APPENDER
appender.console.type = console
appender.console.name = consoleAppender
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%t] %c{1} - %msg%n
# LOGFILE appender
appender.logfile.type = File
appender.logfile.name = logfileAppender
appender.logfile.fileName=extract.log
appender.logfile.layout.type=PatternLayout
appender.logfile.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
logger.example.name = com.example
logger.example.level = debug
logger.example.additivity = true
logger.example.appenderRef.logfile.ref = logfileAppender
logger.info.name = com.example
logger.info.level = info
logger.info.additivity = true
logger.info.appenderRef.console.ref = consoleAppender
# configure the loggers, starting from the root logger.
rootLogger.level = info
But then nothing logs to console.
any ideas?
I also faced with this problem and when I add
rootLogger.appenderRef.console.ref=consoleAppender
rootLogger.appenderRef.rolling.ref=RollingFile
in the end of my log4j2. It's worked. The consoleAppender and RollingFile are the name of the appender.console.name and appender.rolling.name
This is the end of my log4j2 configure:
loggers=console, rolling
logger.console.name=console
logger.console.level=info
logger.console.additivity=false
logger.console.appenderRef.console.ref=consoleAppender
logger.rolling.name=com.viking
logger.rolling.level=info
logger.rolling.additivity=false
logger.rolling.appenderRef.rolling.ref=RollingFile
rootLogger.level=info
rootLogger.additivity=false
rootLogger.appenderRef.console.ref=consoleAppender
rootLogger.appenderRef.rolling.ref=RollingFile
Related
I need to Edit the log4j.properties file and change the log level from INFO to ERROR on log4j.rootCategory but log4j.rootcategory=info doesn't exist
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = console
appender.console.type = Console
appender.console.name = console
appender.console.target = SYSTEM_ERR
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n%ex
I'm currently using log4net and azure files to store my logs, works ace.
I've been searching and can't find any configuration to make the logger create files no bigger than a given KB size.
This is the configuration I have:
<rollingStyle value="Size" />
<MaxSizeRollBackups value="10" />
<MaximumFileSize value="10KB" />
<AzureStorageConnectionString value="connectiondatahere" />
<ShareName value="filelog" />
<Path value="processor" />
<File value="processor_{yyyy-MM-dd}.txt" />
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="%date %-5level %logger %message%newline"/>
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="AzureFileAppender"/>
</root>
I've tried a few variations of this configuration but no luck.
After reviewed the source code of log4net-appender-azurefilestorage, I found the log file size limit is not support in azure file appender currently. I suggest you rewrite the azure file appender by yourself and add the size limit feature.
Below are the steps to do it.
Step 1, Add a property named MaximumFileSize to AzureFileAppender class.
public int MaximumFileSize { get; set; }
Step 2, add the size limit code when appending log to file.
protected override void Append(LoggingEvent loggingEvent)
{
Initialise(loggingEvent);
var buffer = Encoding.UTF8.GetBytes(RenderLoggingEvent(loggingEvent));
if ((_file.Properties.Length + buffer.Length) > MaximumFileSize)
{
//do something if the file reach the max file size
}
else
{
_file.Resize(_file.Properties.Length + buffer.Length);
using (var fileStream = _file.OpenWrite(null))
{
fileStream.Seek(buffer.Length * -1, SeekOrigin.End);
fileStream.Write(buffer, 0, buffer.Length);
}
}
}
Step 3, After that, you could add the size limit(per byte) to configuration file.
<MaximumFileSize value="10240" />
I have a controller where I would like to log to different appenders based on the level. So for examaple general usage would be written to a INFO usage.log. Certain events I determine should go to a WARN warning.log.
Here's a portion of my log4j config…
appenders {
file name: "usageAppender",
file: " /usr/local/logs/cs-wst/usage.log"
file name: "warningAppender",
file: " /usr/local/logs/cs-wst/warning.log"
}
info usageAppender: 'grails.app'
When I do this and run a statement like this in my controller log.info("request method fired") the line does show up in usage.log, but I'm having trouble getting other levels to work. I would like to run these two statements in the controller
log.info("request method fired")
…
log.warn("invalid credentials")
And have them logged to the appropriate file. Can anyone help?
Reference the code below;
appenders {
appender new DailyRollingFileAppender(
name: 'dailyerrorAppender',
threshold: org.apache.log4j.Level.DEBUG,
datePattern: "'.'yyyy-MM-dd", // See the API for all patterns.
fileName: "./error",
layout: pattern(conversionPattern:'%d [%t] %-5p %c{2} %x - %m%n')
)
appender new DailyRollingFileAppender(
name: 'dailywarnAppender',
threshold: org.apache.log4j.Level.WARN,
datePattern: "'.'yyyy-MM-dd", // See the API for all patterns.
fileName: "./warn",
layout: pattern(conversionPattern:'%d [%t] %-5p %c{2} %x - %m%n')
)
}
environments {
development{
debug dailyerrorAppender:"errors", additivity: false
warn dailywarnAppender:"warns", additivity: false
}
qa{
debug dailyerrorAppender:"errors", additivity: false
warn dailywarnAppender:"warns", additivity: false
}
production{
debug dailyerrorAppender:"errors", additivity: false
warn dailywarnAppender:"warns", additivity: false
}
}
def errorLog = Logger.getLogger("errors")
errorLog.debug("your error messages")
def warnLog = Logger.getLogger("warns")
warnLog.warn("your warn messages")
I am trying to log messages to console using Log4j.
I have a class like below
public class Demo {
private static Employee employee=null;
private static Logger logger=Logger.getRootLogger();
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
if(employee!=null){
System.out.println(employee);
}else{
logger.warn("Employee object is null in Demo class main method in if(){} statement");
}
}
}
I have log4j.properties file in the Project folder. If I keep log4j.properties file in classpath I am getting file not found exception , so I kept it in Project folder.My project in eclipse is like below
log4j.properties file is as below
log4j.rootLogger=DEBUG, lntLogger
log4j.appender.lntLogger=org.apache.log4j.ConsoleAppender
log4j.appender.lntLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c - %m --date %d{dd MMM yyyy} %n
Now I am expecting output in the format %-4r [%t] %-5p %c - %m --date %d{dd MMM yyyy} %n
But I am getting Only my message in the console like
Employee object is null in Demo class main method in if(){} statement
Now my question is why my log is not coming in the format what I have specified in properties file.
In log4j.properties file change 4th line to
log4j.appender.lntLogger.layout.ConversionPattern=%-4r [%t] %-5p %c - %m --date %d{dd MMM yyyy} %n
I have the following log4j.xml configuration:
<log4j:configuration>
<appender name = "CONSOLE" class = "org.apache.log4j.ConsoleAppender">
<param name = "Target" value = "System.out"/>
<param name = "Threshold" value = "DEBUG"/>
</appender>
<category name = "com.foo">
<appender-ref ref = "CONSOLE"/>
</category>
</log4j:configuration>
This displays every log in com.foo.* . I want to disable logging in com.foo.bar.* . How do i do this.
By raising the threhold on the com.foo.bar logger:
<category name = "com.foo.bar">
<priority value="WARN"/>
</category>
This logger will be used in preference to the com.foo one, and has a higher threshold will only lets through WARN or higher.