Last active
December 3, 2018 19:22
-
-
Save eduardocl/18206345ce53908f30d38c9624d128b2 to your computer and use it in GitHub Desktop.
Classe para Criar um Log programaticamente
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Adicionar no pom: | |
<dependency> | |
<groupId>org.apache.logging.log4j</groupId> | |
<artifactId>log4j-core</artifactId> | |
<version>2.11.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.logging.log4j</groupId> | |
<artifactId>log4j-slf4j-impl</artifactId> | |
<version>2.11.0</version> | |
</dependency> | |
@Plugin(name = "simple", category = ConfigurationFactory.CATEGORY) | |
@Order(50) | |
public class CustomConfigurationFactory extends ConfigurationFactory { | |
private String filenameSuffix; | |
public CustomConfigurationFactory(String filesuffix) { | |
if (filesuffix == null) { | |
this.filenameSuffix = "log"; | |
} | |
this.filenameSuffix = filesuffix; | |
} | |
Configuration createConfiguration(final String name, ConfigurationBuilder<BuiltConfiguration> builder) { | |
AppenderComponentBuilder console = builder.newAppender("Stdout", "Console"); | |
LayoutComponentBuilder layout = builder.newLayout("PatternLayout").addAttribute("pattern", | |
"[%t] %-5level: %msg%n%throwable"); | |
console.add(layout); | |
builder.add(console); | |
String filename = getFilename(this.filenameSuffix); | |
LayoutComponentBuilder fileLayout = builder.newLayout("PatternLayout").addAttribute("pattern", | |
"%d [%t] %-5level: %msg%n%throwable"); | |
console.add(layout); | |
AppenderComponentBuilder file = builder.newAppender("FileSystem", "File"); | |
file.addAttribute("fileName", filename); | |
file.add(fileLayout); | |
builder.add(file); | |
ComponentBuilder triggeringPolicies = builder.newComponent("Policies") | |
.addComponent(builder.newComponent("CronTriggeringPolicy").addAttribute("schedule", "0 0 0 * * ?")) | |
.addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", "100M")); | |
AppenderComponentBuilder rollingFile = builder.newAppender("rolling", "RollingFile"); | |
rollingFile.addAttribute("fileName", "logs/application-default.log"); | |
rollingFile.addAttribute("filePattern", "application-default-%d{MM-dd-yy}.log.gz"); | |
rollingFile.add(fileLayout); | |
rollingFile.addComponent(triggeringPolicies); | |
builder.add(rollingFile); | |
RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.DEBUG); | |
rootLogger.add(builder.newAppenderRef("Stdout")); | |
rootLogger.add(builder.newAppenderRef("FileSystem")); | |
rootLogger.add(builder.newAppenderRef("rolling")); | |
rootLogger.addAttribute("additivity", false); | |
builder.add(rootLogger); | |
return builder.build(); | |
} | |
private static String getFilename(String filesuffix) { | |
String name = "logs/application-default-$-#.log"; | |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HHmmss"); | |
String s = format.format(new Date()); | |
return name.replace("#", s).replaceAll("\\$", filesuffix); | |
} | |
@Override | |
public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { | |
return getConfiguration(loggerContext, source.toString(), null); | |
} | |
public Configuration getConfiguration(final LoggerContext loggerContext, final String name, | |
final URI configLocation) { | |
ConfigurationBuilder<BuiltConfiguration> builder = newConfigurationBuilder(); | |
return createConfiguration(name, builder); | |
} | |
@Override | |
protected String[] getSupportedTypes() { | |
return new String[] { "*" }; | |
} | |
public void generateXMLConfigurationFile(ConfigurationBuilder<BuiltConfiguration> builder) { | |
try { | |
builder.writeXmlConfiguration(System.out); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public static String printStackTrace(Throwable e) { | |
StringWriter errors = new StringWriter(); | |
e.printStackTrace(new PrintWriter(errors)); | |
return errors.toString(); | |
} | |
} | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.apache.logging.log4j.core.config.ConfigurationFactory; | |
CustomConfigurationFactory customConfigurationFactory = new CustomConfigurationFactory("default"); | |
ConfigurationFactory.setConfigurationFactory(customConfigurationFactory); | |
Logger LOG = LogManager.getLogger(ImportRaisTrabalhadores.class); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment