feat(appender): introduced file-appender app
This commit is contained in:
parent
320194484d
commit
8cbe941e5b
|
@ -0,0 +1,92 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<name>file-appender</name>
|
||||||
|
<description>append text to file</description>
|
||||||
|
<url>https://begerad.de</url>
|
||||||
|
<groupId>de.begerad.file-appender</groupId>
|
||||||
|
<artifactId>file-appender</artifactId>
|
||||||
|
<version>0.0.1</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- https://maven.apache.org/general.html#encoding-warning -->
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<!-- note that Maven Shade Plugin only works with Java from version 7 and up -->
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- junit 5, unit test -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>5.7.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-slf4j-impl</artifactId>
|
||||||
|
<version>2.14.1</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<!--The default maven-surefire-plugin is outdated,
|
||||||
|
make sure update to the latest.-->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.22.2</version>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.4</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<!-- tells that it should be run in package phase -->
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<filters>
|
||||||
|
<filter>
|
||||||
|
<artifact>*:*</artifact>
|
||||||
|
<excludes>
|
||||||
|
<exclude>META-INF/*.MF</exclude>
|
||||||
|
</excludes>
|
||||||
|
</filter>
|
||||||
|
</filters>
|
||||||
|
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||||
|
<shadedClassifierName>depds</shadedClassifierName>
|
||||||
|
<transformers>
|
||||||
|
<!-- configure resources transformer for Maven Shade Plugin with ManifestResourceTransformer that allows us to define a mainClass in the META-INF/MANIFEST.MF file of the jar file, making the jar file after building, can run standalone -->
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<!-- define some other information in the META-INF/MANIFEST.MF file inside the jar file -->
|
||||||
|
<manifestEntries>
|
||||||
|
<Main-Class>de.begerad.fileappender.Main</Main-Class>
|
||||||
|
<Specification-Title>${project.artifactId}</Specification-Title>
|
||||||
|
<Specification-Version>${project.version}</Specification-Version>
|
||||||
|
<Implementation-Title>${project.artifactId}</Implementation-Title>
|
||||||
|
<Implementation-Version>${project.version}</Implementation-Version>
|
||||||
|
<Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id>
|
||||||
|
</manifestEntries>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -0,0 +1,48 @@
|
||||||
|
package de.begerad.fileappender;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
|
||||||
|
public class FileAppender {
|
||||||
|
|
||||||
|
public final static Logger LOG = LoggerFactory.getLogger(FileAppender.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append text line to file
|
||||||
|
*
|
||||||
|
* @param file object representing appending target
|
||||||
|
* @param content object representing content to be appended
|
||||||
|
*/
|
||||||
|
public static void append(File file, String content) {
|
||||||
|
LOG.debug("append() started...");
|
||||||
|
|
||||||
|
// default - create and write
|
||||||
|
// if file not exists, create and write
|
||||||
|
// if file exists, truncate and write
|
||||||
|
/*try (FileWriter fw = new FileWriter(file);
|
||||||
|
BufferedWriter bw = new BufferedWriter(fw)) {
|
||||||
|
bw.write(content);
|
||||||
|
bw.newLine();
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// append mode
|
||||||
|
// if file not exists, create and write
|
||||||
|
// if file exists, append to the end of the file
|
||||||
|
try (FileWriter fw = new FileWriter(file, true);
|
||||||
|
BufferedWriter bw = new BufferedWriter(fw)) {
|
||||||
|
|
||||||
|
bw.write(content);
|
||||||
|
bw.newLine(); // add new line, System.lineSeparator()
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOG.error("ERROR while appending file " + file.getName() + " with content " + content);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG.debug("append() done.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package de.begerad.fileappender;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static de.begerad.fileappender.FileAppender.append;
|
||||||
|
|
||||||
|
//TODO import static de.begerad.fileagerm.FileCleaner.deleteFilesByAge;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static String appendPath = "";
|
||||||
|
|
||||||
|
public static String content = "";
|
||||||
|
|
||||||
|
public final static Logger LOG = LoggerFactory.getLogger(Main.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
LOG.debug("main() started...");
|
||||||
|
|
||||||
|
//check user input for file path
|
||||||
|
if (args.length < 1) {
|
||||||
|
LOG.error("Please enter path to file as first parameter.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
appendPath = args[0];
|
||||||
|
File appendedRecords = new File(appendPath);
|
||||||
|
|
||||||
|
//check user input for content
|
||||||
|
if (args.length < 2) {
|
||||||
|
LOG.error("Please enter content as second parameter.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
content = args[1];
|
||||||
|
|
||||||
|
append(appendedRecords, content);
|
||||||
|
|
||||||
|
LOG.debug("main() done.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Configuration status="INFO">
|
||||||
|
<Appenders>
|
||||||
|
<RollingFile name="rollingFile"
|
||||||
|
fileName="log.txt"
|
||||||
|
filePattern="log-%d{yyyy-MM-dd}.txt"
|
||||||
|
ignoreExceptions="false"
|
||||||
|
>
|
||||||
|
<PatternLayout>
|
||||||
|
<Pattern>[%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} %m%n</Pattern>
|
||||||
|
</PatternLayout>
|
||||||
|
<Policies>
|
||||||
|
<SizeBasedTriggeringPolicy size="3MB" />
|
||||||
|
</Policies>
|
||||||
|
<!--ensure that within the same rollover period no more than X files will be created when a size-based rollover was triggered-->
|
||||||
|
<DefaultRolloverStrategy max="5" />
|
||||||
|
</RollingFile>
|
||||||
|
|
||||||
|
<Console name="console" target="SYSTEM_OUT">
|
||||||
|
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} - %msg%n" />
|
||||||
|
</Console>
|
||||||
|
</Appenders>
|
||||||
|
<Loggers>
|
||||||
|
<Logger name="de.begerad" level="debug" additivity="true">
|
||||||
|
<appender-ref ref="rollingFile" level="debug" />
|
||||||
|
</Logger>
|
||||||
|
<Root level="debug" additivity="false">
|
||||||
|
<appender-ref ref="console" />
|
||||||
|
</Root>
|
||||||
|
</Loggers>
|
||||||
|
|
||||||
|
</Configuration>
|
|
@ -0,0 +1,8 @@
|
||||||
|
package de.begerad.fileappender;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class TestFileAppender {
|
||||||
|
}
|
Loading…
Reference in New Issue