Logback - do not create empty log files at startup - startup

I have a project with a lot of 'tool' classes that have their own logging. Those logfiles are created at startup of the application, but remain empty, until used.
Is it possible to tell logback that empty files should not be created at startup? But only when they are being used?
Somehow I don't find information on this topic. Thanks!

There is no official support for lazy/on-demand creation of log files in Logback's FileAppender.
However there are some known configuration workarounds that may achieve the same result. For more details see the Logback feature request 202 "FileAppender should permit lazy file creation".
My personal favorite is the variant using the LazyFileOutputStream and a custom implementation of a FileAppender. A working implementation of an LazyFileOutputStream can be found in Alessio Pollero's log4j-additions section.
An the LazyFileappender code is very simple:
public class LazyFileAppender<E> extends FileAppender<E> {
#Override
public void openFile(String file_name) throws IOException {
lock.lock();
try {
File file = new File(file_name);
boolean result = FileUtil.createMissingParentDirectories(file);
if (!result) {
addError("Failed to create parent directories for [" + file.getAbsolutePath() + "]");
}
LazyFileOutputStream lazyFos = new LazyFileOutputStream(file, append);
setOutputStream(lazyFos);
} finally {
lock.unlock();
}
}
}

Related

Rejecting published file request for file that has not been published. Tried all the options but didn't work

I am trying to implement a sample application where all the javascript (JS) & CSS files use many png files.
I referred many articles but they could not help me.
For all the png files, I get the following error,
Sample error part,
Jan 29, 2019 3:25:22 PM com.vaadin.server.communication.PublishedFileHandler handleRequest
WARNING: Rejecting published file request for file that has not been published: css/images/chartIcon.png
Jan 29, 2019 3:25:22 PM com.vaadin.server.communication.PublishedFileHandler handleRequest
WARNING: Rejecting published file request for file that has not been published: css/images/sunburst.png
Jan 29, 2019 3:25:22 PM com.vaadin.server.communication.PublishedFileHandler handleRequest
WARNING: Rejecting published file request for file that has not been published: css/images/treemap.png
Jan 29, 2019 3:25:40 PM com.vaadin.server.communication.PublishedFileHandler handleRequest
WARNING: Rejecting published file request for file that has not been published: css/images/sprite.png
The following is the folder structure that I have,
de.qsoft.manatee.web.vaadin.myapp
de.qsoft.manatee.web.vaadin.myapp.css --> Contains all CSS files
de.qsoft.manatee.web.vaadin.myapp.fileMenu --> Contains all CSS files
de.qsoft.manatee.web.vaadin.myapp.widgets --> Contains all CSS files
de.qsoft.manatee.web.vaadin.myapp.scripts --> contains all js files
de.qsoft.manatee.web.vaadin.myapp.widgets --> contains all js files
de.qsoft.manatee.web.vaadin.myapp.colorpicker --> contains all js files
SpreadJSWidget.Java
#JavaScript({
"scripts/jquery-1.11.1.min.js",
"scripts/jquery-ui-1.10.3.custom.min.js",
"spreadjs_connector.js",
"colorpicker/colorPicker.js",
"fileMenu/fileMenu.js",
"scripts/actionmanager.js",
"scripts/app.js",
"scripts/bootstrap.min.js",
"scripts/FileSaver.min.js",
"scripts/gc.spread.excelio.12.0.5.min.js",
"scripts/gc.spread.sheets.all.12.0.5.min.js",
"scripts/gc.spread.sheets.barcode.12.0.5.min.js",
"scripts/gc.spread.sheets.charts.12.0.5.min.js",
"scripts/gc.spread.sheets.pdf.12.0.5.min.js",
"scripts/gc.spread.sheets.print.12.0.5.min.js",
"scripts/gc.spread.sheets.shapes.12.0.5.min.js",
"scripts/license.js",
"scripts/resources.js",
"scripts/ribbon-data.js",
"scripts/ribbon.js",
"scripts/sample.js",
"scripts/spreadActions.js",
"scripts/util.js",
"widgets/addChartElement/chartAddChartElement.js",
"widgets/chartColorPicker/chart-colorPicker.js",
"widgets/chartLayoutPicker/chartLayoutPicker.js",
"widgets/richText/richTextEditor.js"
})
#StyleSheet({
"colorpicker/colorPicker.css",
"css/font-awesome/css/font-awesome.min.css",
"css/bootstrap-theme.min.css",
"css/bootstrap.min.css",
"css/borderpicker.css",
"css/colorpicker.css",
"css/excel2013.css",
"css/gc.spread.sheets.12.0.5.css",
"css/gc.spread.sheets.excel2013white.12.0.5.css",
"css/insp-slicer-format.css",
"css/insp-table-format.css",
"css/inspector.css",
"css/sample.css",
"css/shapes.css",
"fileMenu/fileMenu.css",
"widgets/addChartElement/chartAddChartElement.css",
"widgets/chartColorPicker/chart-colorPicker.css",
"widgets/chartLayoutPicker/chartLayoutPicker.css",
"widgets/richText/richTextWithRichEditor.css",
})
public class SpreadJSWidget extends AbstractJavaScriptExtension
{
/**
*
*/
private static final long serialVersionUID = -804316208810859887L;
public interface ValueChangeListener extends Serializable {
void valueChange();
}
ArrayList<ValueChangeListener> listeners = new ArrayList<ValueChangeListener>();
public void addValueChangeListener(ValueChangeListener listener) {
listeners.add(listener);
}
/**
*
*/
public SpreadJSWidget() {
// TODO hari: Auto-generated constructor stub
}
/*'***************************************************************************************
* Static/Inner class members
******************************************************************************************/
/*'***************************************************************************************
* Class members
******************************************************************************************/
public void setValue(String value) {
getState().value = value;
}
#Override
protected void extend(AbstractClientConnector target) {
// TODO hari: Not Yet Implemented
super.extend(target);
}
public String getValue() {
return getState().value;
}
#Override protected SpreadJSWidgetState getState() {
return (SpreadJSWidgetState) super.getState();
}
}
I tried the following step by step but i could not get expected results,
I have kept all the png files under the directory "de.qsoft.manatee.web.vaadin.myapp.css" as "de.qsoft.manatee.web.vaadin.myapp.css.images"
Under VAADIN folder, i copied all the png files as "VAADIN/css/images/"
Under VAADIN folder like "VAADIN/themes/mytheme/img/css/images"
Under VAADIN folder like "VAADIN/themes/mytheme/layouts/css/images"
#Theme("mytheme")
public class MyUI extends UI {
#Override
protected void init(VaadinRequest vaadinRequest) {
SpreadWidget widget = new SpreadWidget();
setContent(widget);
}
#WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
#VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}
Please let me know where should i put all png files. Each css is refering image file as
http://localhost:8080/spreadjs/APP/PUBLISHED/css/images/AllShapes.png
What happens here is caused by a security feature. Since the files are in this case served directly from the classpath, Vaadin takes some precautions to prevent accidentally also publishing other things from the classpath, e.g. something like DatabaseConnection.java which might contain sensitive passwords.
For this reason, only files that are explicitly published using a #StyleSheet, #JavaScript or #HtmlImport annotation are available. Since there is no corresponding annotation for e.g. css/images/AllShapes.png, the server ignores those requests.
I'm aware of a couple of potential workarounds in this kind of case, but neither is really elegant:
Put the images in e.g. VAADIN/css/images and update the CSS to use an appropriate number of ../ segments to cancel out the /APP/PUBLISHED/ part of the URL. The URL in the CSS would thus be something along the lines of ../../VAADIN/css/images.AllShapes.png.
Put the CSS along with the images in VAADIN/. In that way, you don't need to change the URLs that refer to the images, but you instead need to manually load the CSS instead of relying on the convenient #StyleSheet annotation. In that case, I'd recommend using something like ui.getPage().getStyles().add(new ThemeResource("../../css/name.css"));. The ../../ part is to cancel out themes/mytheme/ that will automatically be used for a theme resource. You could do this in e.g. the attach() method (just remember to also call super.attach()). You should preferably also add some logic that only adds the dependency if it hasn't already been done previously for the same UI instance.
Use the internal LegacyCommunicationManager.registerDependency method to also register your images to be available directly from the classpath. You can find an instance to LegacyCommunicationManager using vaadinSession.getCommunicationManager().
As an unrelated note, I'd recommend combining the different scripts and CSS files into a single file of each type. The reason for this is that loading lots of small files over HTTP causes some performance overhead that can be avoided by bundling the files together.

How to customize an existing Grails plugin functionality, modifying behavior of doWithSpring method

I am new to grails and while working with Spring Security LDAP plugin it was identified that it accepts the ldap server password in plain text only. The task in hand is to pass an encrypted password which is decrypted before it is consumed by the plugin during its initialization phase.
I have already searched for all possible blogs and stackoverflow questions but could not find a way to extend the main plugin class to simply override the doWithSpring() method so that i can simply add the required decryption logic for the Ldap server password. Any help here will be appreciated.
I have already seen and tried jasypt plugin but it also does not work well if the password is stored in some external file and not application yml. So I am looking for a solution to extend the Spring security plugin main class, add the required behavior and register the custom class.
EDIT
Adding the snippet from Grails LDAP Security plugin, which I am trying to override. So If i am successfully able to update the value of securityConfig object before the plugin loads, the purpose is solved.
Some snippet from the plugin:
def conf = SpringSecurityUtils.securityConfig
...
...
contextSource(DefaultSpringSecurityContextSource, conf.ldap.context.server) { // 'ldap://localhost:389'
authenticationSource = ref('ldapAuthenticationSource')
authenticationStrategy = ref('authenticationStrategy')
userDn = conf.ldap.context.managerDn // 'cn=admin,dc=example,dc=com'
**password = conf.ldap.context.managerPassword // 'secret'**
contextFactory = contextFactoryClass
dirObjectFactory = dirObjectFactoryClass
baseEnvironmentProperties = conf.ldap.context.baseEnvironmentProperties // none
cacheEnvironmentProperties = conf.ldap.context.cacheEnvironmentProperties // true
anonymousReadOnly = conf.ldap.context.anonymousReadOnly // false
referral = conf.ldap.context.referral // null
}
ldapAuthenticationSource(SimpleAuthenticationSource) {
principal = conf.ldap.context.managerDn // 'cn=admin,dc=example,dc=com'
**credentials = conf.ldap.context.managerPassword // 'secret'**
}
You don't need to override the doWithSpring() method in the existing plugin. You can provide your own plugin which loads after the one you want to affect and have your doWithSpring() add whatever you want to the context. If you add beans with the same name as the ones added by the other plugin, yours will replace the ones provided by the other plugin as long as you configure your plugin to load after the other one. Similarly, you could do the same think in resources.groovy of the app if you don't want to write a plugin for this.
You have other options too. You could write a bean post processor or bean definition post processor that affects the beans created by the other plugin. Depending on the particulars, that might be a better idea.
EDIT:
After seeing your comment below I created a simple example that shows how you might use a definition post processor. See the project at https://github.com/jeffbrown/postprocessordemo.
The interesting bits:
https://github.com/jeffbrown/postprocessordemo/blob/master/src/main/groovy/demo/SomeBean.groovy
package demo
class SomeBean {
String someValue
}
https://github.com/jeffbrown/postprocessordemo/blob/master/src/main/groovy/demo/SomePostProcessor.groovy
package demo
import org.springframework.beans.BeansException
import org.springframework.beans.MutablePropertyValues
import org.springframework.beans.PropertyValue
import org.springframework.beans.factory.config.BeanDefinition
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory
import org.springframework.beans.factory.support.BeanDefinitionRegistry
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor
class SomePostProcessor implements BeanDefinitionRegistryPostProcessor{
#Override
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
BeanDefinition definition = registry.getBeanDefinition('someBean')
MutablePropertyValues values = definition.getPropertyValues()
PropertyValue value = values.getPropertyValue('someValue')
def originalValue = value.getValue()
// this is where you could do your decrypting...
values.addPropertyValue('someValue', "MODIFIED: ${originalValue}".toString())
}
#Override
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
https://github.com/jeffbrown/postprocessordemo/blob/master/grails-app/conf/spring/resources.groovy
beans = {
someBean(demo.SomeBean) {
someValue = 'Some Value'
}
somePostProcessor demo.SomePostProcessor
}
https://github.com/jeffbrown/postprocessordemo/blob/master/grails-app/init/postprocessordemo/BootStrap.groovy
package postprocessordemo
import demo.SomeBean
class BootStrap {
SomeBean someBean
def init = { servletContext ->
log.info "The Value: ${someBean.someValue}"
}
def destroy = {
}
}
At application startup you will see log output that looks something like this...
2017-10-23 19:04:54.356 INFO --- [ main] postprocessordemo.BootStrap : The Value: MODIFIED: Some Value
The "MODIFIED" there is evidence that the bean definition post processor modified the property value in the bean. In my example I am simply prepending some text to the string. In your implementation you could decrypt a password or do whatever you want to do there.
I hope that helps.
After trying Jasypt plugin and BeanPostProcessor solutions unsuccessfully for my use case, I found below solution to work perfectly.
To describe again the problem statement here,
a) we had to keep the passwords in an encrypted format inside properties files
b) and given we were packaging as a war file so the properties must not be kept inside the war to allow automated deployment scripts update the encrypted passwords depending on the environment
Jasypt plugin was a perfect solution for the use case a), but it was not able to cover the b) scenario
Moreover, the Grails LDAP Security plugin was getting loaded quite early hence Bean Post processors were also not helping out here.
Solution:
Created a new class by implementing the interface SpringApplicationRunListener. Extended its methods and parsed the properties file using YamlPropertySourceLoader
Sample code:
YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
PropertySource<?> applicationYamlPropertySource = loader.load(
"application.yml", new ClassPathResource("application.yml"),"default");
return applicationYamlPropertySource;
Once the properties were loaded inside the MapPropertySource object, parsed them for the encrypted values and applied the decryption logic.
This whole implementation was executed before any plugins were initialized during Grails bootup process solving the purpose.
Hope it will help others.

How to populate parameter "defaultValue" in Maven "AbstractMojoTestCase"?

I have a Maven plugin that I am attempting to test using a subclass of the AbstractMojoTestCase. The plugin Mojo defines an outputFolder parameter with a defaultValue. This parameter is not generally expected to be provided by the user in the POM.
#Parameter(defaultValue = "${project.build.directory}/someOutputFolder")
private File outputFolder;
And if I use the plugin in a real scenario then the outputFolder gets defaulted as expected.
But if I test the Mojo using the AbstractMojoTestCase then while parameters defined in the test POM are populated, parameters with a defaultValue that are not defined in the POM are not populated.
public class MyPluginTestCase extends AbstractMojoTestCase {
public void testAssembly() throws Exception {
final File pom = getTestFile( "src/test/resources/test-pom.xml");
assertNotNull(pom);
assertTrue(pom.exists());
final MyMojo myMojo = (BaselineAssemblyMojo) lookupMojo("assemble", pom);
assertNotNull(myMojo);
myMojo.execute(); // Dies due to NullPointerException on outputFolder.
}
}
Further: if I define the outputFolder parameter in the POM like so:
<outputFolder>${project.build.directory}/someOutputFolder</outputFolder>
then ${project.build.directory} is NOT resolved within the AbstractMojoTestCase.
So what do I need to do to get the defaultvalue populated when testing?
Or is this a fault in the AbstractMojoTestCase?
This is Maven-3.2.3, maven-plugin-plugin-3.2, JDK 8
You need to use lookupConfiguredMojo.
Here's what I ended up using:
public class MyPluginTest
{
#Rule
public MojoRule mojoRule = new MojoRule();
#Test
public void noSource() throws Exception
{
// Just give the location, where the pom.xml is located
MyPlugin plugin = (MyPlugin) mojoRule.lookupConfiguredMojo(getResourcesFile("basic-test"), "myGoal");
plugin.execute();
assertThat(plugin.getSomeInformation()).isEmpty();
}
public File getResourcesFile(String filename)
{
return new File("src/test/resources", filename);
}
}
Of course you need to replace myGoal with your plugin's goal. You also need to figure out how to assert that your plugin executed successfully.
For a more complete example, check out the tests I wrote for fmt-maven-plugin

How to force log4j2 rolling file appender to roll over?

To my best knowledge, RollingFileAppender in log4j2 will not roll over at the specified time (let's say - at the end of an hour), but at the first log event that arrives after the time threshold has been exceeded.
Is there a way to trigger an event, that on one hand will cause the file to roll over, and on another - will not append to the log (or will append something trivial, like an empty string)?
No there isn't any (built-in) way to do this. There are no background threads monitoring rollover time.
You could create a log4j2 plugin that implements org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy (See the built-in TimeBasedTriggeringPolicy and SizeBasedTriggeringPolicy classes for sample code.)
If you configure your custom triggering policy, log4j2 will check for every log event whether it should trigger a rollover (so take care when implementing the isTriggeringEvent method to avoid impacting performance). Note that for your custom plugin to be picked up, you need to specify the package of your class in the packages attribute of the Configuration element of your log4j2.xml file.
Finally, if this works well for you and you think your solution may be useful to others too, consider contributing your custom triggering policy back to the log4j2 code base.
Following Remko's idea, I wrote the following code, and it's working.
package com.stony;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.rolling.*;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
#Plugin(name = "ForceTriggerPolicy", category = "Core")
public class ForceTriggerPolicy implements TriggeringPolicy {
private static boolean isRolling;
#Override
public void initialize(RollingFileManager arg0) {
setRolling(false);
}
#Override
public boolean isTriggeringEvent(LogEvent arg0) {
return isRolling();
}
public static boolean isRolling() {
return isRolling;
}
public static void setRolling(boolean _isRolling) {
isRolling = _isRolling;
}
#PluginFactory
public static ForceTriggerPolicy createPolicy(){
return new ForceTriggerPolicy();
}
}
If you have access to the Object RollingFileAppender you could do something like:
rollingFileAppender.getManager().rollover();
Here you can see the manager class:
https://github.com/apache/logging-log4j2/blob/d368e294d631e79119caa985656d0ec571bd24f5/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java

How can i convert a FilePath to a File?

I'm writing a Jenkins plugin and i'm using build.getWorkspace() to get the path to the current workspace. The issue is that this returns a FilePath object.
How can i convert this to a File object?
Although I haven't tried this, according to the javadoc you can obtain the URI from which you can then create a file: File myFile = new File(build.getWorkspace().toURI())
Please use the act function and call your own FileCallable implementation if your plugin should work for master and slaves. For more information check the documentation, chapter "Using FilePath smartly" or this stackoverflow answer.
Code example (source):
void someMethod(FilePath file) {
// make 'file' a fresh empty directory.
file.act(new Freshen());
}
// if 'file' is on a different node, this FileCallable will
// be transferred to that node and executed there.
private static final class Freshen implements FileCallable<Void> {
private static final long serialVersionUID = 1;
#Override public Void invoke(File f, VirtualChannel channel) {
// f and file represent the same thing
f.deleteContents();
f.mkdirs();
return null;
}
}
This approach
File myFile = new File(build.getWorkspace().toURI()) is not the correct solution. I don't know why this has been an accepted anwser till date.
The approach mentioned by Sascha Vetter is correct, taking the reference from official Jenkins javadocs
,which clearly says and I quote
Unlike File, which always implies a file path on the current computer, FilePath represents a file path on a specific agent or the controller.
So being an active contributor to Jenkins community, I would reference the answer given by Sascha Vetter.
PS. Reputation point criteria makes me unable to up-vote the correct answer.

Resources