Part of my code:
public class TestContext {
private static TestContext textContext = null;
private WebDriver driver;
static ConfigFileReader configFileReader = new ConfigFileReader();
private static Logger log = LogManager.getLogger(TestContext.class.getName());
public static void main(String[] args)
{
log.debug("Debug Message Logged !!!");
log.info("Info Message Logged !!!");
log.error("Error Message Logged !!!");
}
In console when I ran it:
17:17:49.037 [main] ERROR Util.TestContext - Error Message Logged !!!
Properties file:
status = error
name = PropertiesConfig
#Make sure to change log file path as per your need
property.filename = C:/logs/debug.log
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling
#Make sure to change the package structure as per your application
logger.rolling.name = com.howtodoinjava
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
Where the configuration file:
enter image description here
Dependencies:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
is it because it can not locate the properties file???
Problem is solved when I put the properties file under src/test/resources directly.
Related
I have a simple script that takes coordinates and turns them into points.
df.withColumn("geometry", expr("ST_Point(CAST(home_top_1_longitude AS Decimal(24,20)), CAST(home_top_1_latitude AS Decimal(24,20)))"))
When I run it on a cluster, it works and I get a df with POINTS.
+--------------------+
| geometry|
+--------------------+
|POINT (37.77244 5...|
|POINT (37.707264 ...|
+--------------------+
When I try to test it localy, I get this error.
An exception or error caused a run to abort: org.apache.spark.sql.catalyst.analysis.FunctionRegistry.createOrReplaceTempFunction(Ljava/lang/String;Lscala/Function1;)V
java.lang.NoSuchMethodError: org.apache.spark.sql.catalyst.analysis.FunctionRegistry.createOrReplaceTempFunction(Ljava/lang/String;Lscala/Function1;)V
This is what my test looks like.
class TestApplication extends FunSuite with DataFrameSuiteBase{
private implicit var config: Configuration = _
private val pathSources = "src/test/resources/"
val tb: String = pathSources + "tb.csv"
override def beforeAll(): Unit = {
super.beforeAll()
config = new Configuration(TestParams.args)
GeoSparkSQLRegistrator.registerAll(spark)
loadTestTables(spark, config)
}
def loadTestTables(implicit spark: SparkSession, conf: Configuration): Unit = {
createTable(tb, "tb")
}
def createTable(path: String, tableName: String)(implicit spark: SparkSession): Unit = {
spark.read
.format("com.databricks.spark.csv")
.option("header", "true")
.option("delimiter", ";")
.option("nullValue", "")
.load(path)
.createOrReplaceTempView(tableName)
}
test("tb") {
import spark.implicits._
implicit val sparkSession: SparkSession = spark
spark.table("tb").withColumn("geometry", expr("ST_Point(CAST(long AS Decimal(24,20)), CAST(lat AS Decimal(24,20)))"))
}
}
I thought there was a confict in my dependencies, but it wouldn't have worked on cluster if that was the case. These are the dependencies I use.
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_2.11</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.holdenkarau</groupId>
<artifactId>spark-testing-base_2.11</artifactId>
<version>2.2.0_0.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.datasyslab</groupId>
<artifactId>geospark-sql_2.3</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.datasyslab</groupId>
<artifactId>geospark</artifactId>
<version>1.3.1</version>
</dependency>
I am trying to route the console logging to a file. Though, I was able to generate the log file but it's not capturing the output generated from logger.info . The output is getting printed on the console but the same is not reflecting in the log file.
application.properties :
logging.file.path= log
logging.pattern.file= [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
Code :
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
#Service
public class GetImageFiles {
#Value("${app.images.base.url}")
String imagesBaseUrl;
#Autowired
DataAccessor dataAccessor;
private static Logger logger = LoggerFactory.getLogger(GetImageFiles.class);
public void getImageDetails(String sessionID, String imageId, String articleId){
HashMap<String, String> map = new HashMap<>();
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("X-Chorus-Session",sessionID);
HttpEntity<String> request = new HttpEntity<>(null, headers);
String imageFilesUrl = imagesBaseUrl + "/" + imageId;
ResponseEntity<String> responseEntity = restTemplate.exchange(imageFilesUrl, HttpMethod.GET, request, String.class);
JSONObject jsonObject = new JSONObject(responseEntity.getBody());
String imageName = jsonObject.get("filename").toString();
JSONObject thumbnails = (JSONObject) jsonObject.get("thumbnails");
JSONObject thumbnailsSize = (JSONObject) thumbnails.get("large");
String imageUrl = thumbnailsSize.get("url").toString();
map.put(imageName, imageUrl);
for(Map.Entry<String, String > mapValue : map.entrySet()) {
if (mapValue.getKey().lastIndexOf(".") > 0) {
String imageMapKey = mapValue.getKey().substring(0, mapValue.getKey().lastIndexOf("."));
if (imageMapKey.equals(articleId)) {
logger.info("Processing : The image name is : " + mapValue.getKey() + " and the imager URL is :" + mapValue.getValue() + " for article Id is :" + articleId);
dataAccessor.updateImageDetails(mapValue.getKey(), mapValue.getValue(), articleId);
}
}
}
}
}
pom.xml :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
Console output ..
2022-03-09 17:17:00.110 INFO 32900 --- [ restartedMain] o.s.b.w.e.t.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-09 17:17:00.122 INFO 32900 --- [ restartedMain] c.a.t.ThirdlightApplication : Started ThirdlightApplication in 11.293 seconds (JVM running for 12.775)
Total Article Ids to be processed :10
There is no image ids to be processed for article id :52KTM41
There is no image ids to be processed for article id :52LGY10
There is no image ids to be processed for article id :52NAZ41
There is no image ids to be processed for article id :52NBT60
There is no image ids to be processed for article id :52NEK40
There is no image ids to be processed for article id :52QAZ30
There is no image ids to be processed for article id :54LAQ01
There is no image ids to be processed for article id :54LAQ20
There is no image ids to be processed for article id :56NAK43
There is no image ids to be processed for article id :56NEU40
The process has been completed !
spring.log :
[INFO ] 2022-03-09 17:20:49.920 [restartedMain] ThirdlightApplication - Starting ThirdlightApplication using Java 11.0.7 on LHTU05CD9032TMM with PID 6456 (C:\Users\psingh69\Downloads\thirdlight\target\classes started by psingh69 in C:\Users\psingh69\Downloads\thirdlight)
[INFO ] 2022-03-09 17:20:49.933 [restartedMain] ThirdlightApplication - No active profile set, falling back to 1 default profile: "default"
[INFO ] 2022-03-09 17:20:50.000 [restartedMain] DevToolsPropertyDefaultsPostProcessor - Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
[INFO ] 2022-03-09 17:20:50.000 [restartedMain] DevToolsPropertyDefaultsPostProcessor - For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
[INFO ] 2022-03-09 17:20:59.846 [restartedMain] TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
[INFO ] 2022-03-09 17:20:59.994 [restartedMain] ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 9993 ms
[INFO ] 2022-03-09 17:21:00.670 [restartedMain] OptionalLiveReloadServer - LiveReload server is running on port 35729
[INFO ] 2022-03-09 17:21:00.723 [restartedMain] TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
[INFO ] 2022-03-09 17:21:00.742 [restartedMain] ThirdlightApplication - Started ThirdlightApplication in 11.588 seconds (JVM running for 13.107)
In my case I had to put the configuration file "logback.xml" inside the folder src/main/resources...
It is highly customizable but you can start with something like this (both STDOUT - the console - and a FILE are simultaneously written):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{0} - %msg \(%file:%line\)%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/yourappname.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/yourappname.%d{yyyy-MM-dd}.log.tar.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss'Z'} - %m%n</pattern>
</encoder>
</appender>
<root level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
The example above writes an "yourappname.log" file inside the "/logs" folder (C:\logs\yourappname.log if you're using Windows and your app is running on drive C:, on /logs/yourappname.log if you're in Linux or MacOS).
Furthermore, the above configuration file provides a time-based LogRotation mechanism (which means that, at regular time intervals - 7 days in this case - the logfile is compressed and archived in a tar.gz fashion and a new one is started fresh.)
I have no idea about the opportunity to put that configuration string inside the application.properties, sorry...
Slightly different from yours, my pom.xml refers to an alternative log framework (ch.qos.logback), here it is:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.11</version>
</dependency>
This is a personal project and I just want this to work.
I have a Java 8 application that is using Log4j 2 and all I want to do is get it to log to one of the general Tomcat 9 logs like catalina.out or localhost or the tomcat stdout log. Nothing I do seems to work.
Log4J is included in the war file, I only need it to work for this one application under Tomcat 9. I do not want to move to another logging library. This is my log4j.properties file.
log4j.rootCategory=debug,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.immediateFlush=true
log4j.appender.console.encoding=UTF-8
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%d [%t] %-5p %c - %m%n
This is an example of how I'm using this in my code:
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public final class DataController {
private static Logger LOG = LogManager.getLogger(DataController.class);
public void exampleMethod() {
LOG.info("Log something");
}
}
Nothing shows up in any log. Tomcat 9 is a stock install and has a stock logging.properties file. This is NOT a Spring application. It is pure Java Servlets and POJO. For further context, I did originally use a file appender and logged to a custom file. That didn't work either. By the way, although I know some of the terminology, I am a complete noob at Java.
I was using a log4j 1.2 properties file. This sample properties file worked.
status = debug
name = PropertiesConfig
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
Develop a spring boot application in 2.2.1 RELEASE.Everything is working fine except Loging using log4j.properties.
In apoplication.properies, added logging.config as given below
logging.config =${external.config}/log4j.properties.
Pom.xml file,excluded spring-boot-starter-logging and added spring-boot-starter-log4j as given below
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
log4j.properties
log4j.rootLogger=error,Service
# Direct log messages to a log file
log4j.appender.Service=org.apache.log4j.RollingFileAppender
log4j.appender.Service.File=C:/log/Service.log
log4j.appender.Service.MaxFileSize=1MB
log4j.appender.Service.MaxBackupIndex=1
log4j.appender.Service.layout=org.apache.log4j.PatternLayout
log4j.appender.Service.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - [%X{AUDIT_USER}] %m%n
i referred following links in stack
1.Logger not printing with log4j.properties within Spring Boot 1.5.7
2.Spring boot logging with log4.properties file in not working
Edit 1
As Andy Wilkinson comments, i modified log4j.properties file content and file name, file name should be log4j2.properties.
I tried to log some string to log file as given below
public class ServiceMain {
private static final Logger logger = LogManager.getLogger(ServiceMain.class);
public static void main(String[] args) {
SpringApplication.run(ServiceMain.class, args);
logger.debug("----------------------Stating spring booot----------------------");
}
}
debug log "----------------------Stating spring booot----------------------" wont write to c:/log/service.log
Log file
2019-11-22 05:20:14,631 INFO o.s.b.StartupInfoLogger [main] Starting ServiceMain v7.0.0.0 on host-4 with PID 119176 (D:\Service\target\Service-1.0.jar started by Administrator in D:\Service\target)
2019-11-22 05:20:14,634 DEBUG o.s.b.StartupInfoLogger [main] Running with Spring Boot v2.2.1.RELEASE, Spring v5.2.1.RELEASE
2019-11-22 05:20:14,635 INFO o.s.b.SpringApplication [main] The following profiles are active: Service
2019-11-22 05:20:24,898 INFO o.s.b.StartupInfoLogger [main] Started ServiceMain in 12.312 seconds (JVM running for 14.189)
Did i miss any log4j2.properties configuration?
You are using Log4j 2 (which you should be as version 1 is no longer supported) but appear to be configuring it using a Log4j 1 configuration file.
You can learn more about Log4j 2’s configuration properties in its documentation. It includes this example:
status = error
dest = err
name = PropertiesConfig
property.filename = target/rolling/rollingtest.log
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = error
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = target/rolling2/test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
logger.rolling.name = com.example.my.app
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT
I want to use log4j for my app (I don't need to handle the server logs for now). I searched for a solution but all post are related with server logs. When I run the app, the log4j.properties is read and the logs file are created but it didn't write on them.
I'm using Tomee as server. I have other project with Wildfly using the same solution for log4j and it works.
log4j.properties:
log4j.appender.TEST = org.apache.log4j.DailyRollingFileAppender
log4j.appender.TEST.File = /opt/share/test-project/logs/test.log
log4j.appender.TEST.DatePattern = '.'yyyy-MM-dd
log4j.appender.TEST.layout = org.apache.log4j.PatternLayout
log4j.appender.TEST.layout.ConversionPattern = [%d{ISO8601}] %5p - %x - %c.%M(%L): %m%n
# Define the types of logger and level of logging
#log4j.rootLogger.org = DEBUG,CONSOLE
log4j.logger.com.test=DEBUG, TEST
Log4jConfig class which is initialized:
import java.io.File;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.log4j.PropertyConfigurator;
#WebListener
public class Log4jConfig implements ServletContextListener {
private final static String LO4G_FILENAME="/opt/share/test-project/config/log4j.properties";
private static final Logger LOGGER = LoggerFactory.getLogger(Log4jConfig.class);
...
...
public void contextInitialized(ServletContextEvent arg0)
{
try{
File file = new File(LO4G_FILENAME);
if (file.exists())
{
PropertyConfigurator.configure(LO4G_FILENAME);
LOGGER.info("[Log4JInitServlet - contextInitialized] - Log4J configured:"+LO4G_FILENAME);
}
}catch (Exception e) {
LOGGER.error("[Log4JInitServlet - contextInitialized] - Exception >>",e);
}
}
}
Test.java:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
#Path("/test")
public class TestWs {
private static final Logger LOGGER = LoggerFactory.getLogger(TestWs.class.getName());
//here LOGGER is null. If I put it into test() is not null but not write on the log either.
#GET
#Path("/")
#Produces(MediaType.APPLICATION_JSON)
public Response test(){
LOGGER.debug("[TestWs - init] - init");
long currentSystemTime = System.currentTimeMillis();
LOGGER.error("[TestWs - test] - Error: TEST");
LOGGER.debug("[TestWs - test] - Finish Timing:"+(System.currentTimeMillis()-currentSystemTime));
return Response.ok().build();
}
}
POM.xml:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.12</version>
</dependency>
Any idea?
upgrade dependency of slf4f :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
use private static final Logger logger = LoggerFactory.getLogger(class-name.class); into class in which you want to create logs.
use bellow log4j configuration :-
Root logger option
log4j.rootLogger=INFO, file
Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./logs/log.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.logger.org.springframework=WARN
log4j.logger.com.amstech=DEBUG
I'm not sure of all the setup but if you put log4j lib/config in the container you can need to set in conf/system.properties:
openejb.logger.external=true
otherwise (for legacy reasons), tomee will setup log4j in a custom way making your configuration not respected. This property just means "use normal log4j behavior".