Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request - filter stacktrace #79

Closed
Yjuq opened this issue Jul 10, 2018 · 3 comments
Closed

Feature request - filter stacktrace #79

Yjuq opened this issue Jul 10, 2018 · 3 comments
Assignees
Milestone

Comments

@Yjuq
Copy link

Yjuq commented Jul 10, 2018

Hey :)

A option to filter the stacktrace would be great. Exceptions are always long and the most informations are not needed. It would be great if I can choose in the tinylog.properties which packages information should be printed with TinyLog.

Something like this:
tinylog.stacktrace.filter = acg|javafx.scene - This could be print only informations about the package acg (and sub packages like acg.* etc.) and all information in the package javafx.scene.*

Maybe a other option with this feature would be nice. To remove the "caused by" information in exceptions (just don't print it):

java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
        at acg.component.Card.<init>(Card.java:38)
        at acg.Applet.start(Applet.java:17)
< This Informations are not needed: >
    Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
        at javafx.scene.image.Image.validateUrl(Image.java:1110)
        at javafx.scene.image.Image.<init>(Image.java:620)
        at acg.component.Card.<init>(Card.java:38)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        ...

The option for that could be: 'tinylog.stacktrace.cause=true|false' to turn it on or off.


Currently I use a extern class to filer the informations with a regex pattern. Maybe it helps you a bit to develop an idea for your project:

public class StackTrace {
	private static String pattern = "^acg\\..*$"; // Filter for acg.* package
	
	// ++++++++++ +++++++++ ++++++++ +++++++ ++++++ +++++ ++++ +++ ++ +
	
	private StackTrace() {
		// Nothing to do
	}
	
	// ++++++++++ +++++++++ ++++++++ +++++++ ++++++ +++++ ++++ +++ ++ +
	
	public static void filter(Exception exception) {
		Stack<StackTraceElement> stack = new Stack<StackTraceElement>();
		StackTraceElement[] ste = exception.getStackTrace();
		
		for (int i = 0; i < ste.length; ++i) {
			if (ste[i].getClassName().matches(pattern)) {
				stack.push(ste[i]);
			}
		}
		
		exception.setStackTrace(stack.toArray(new StackTraceElement[stack.size()]));
	}
}

I can't remove the "caused by" information by myself without changing your sourcecode from your Logger class.

If you need help with this - I could maybe implement this feature by myself and share the source with you. So you can add this to your project.

Nice work by the way. I like your logger :)

@pmwmedia
Copy link
Member

pmwmedia commented Jul 15, 2018

I've thought about it and made a first draft that includes your use cases and some of my ideas that I've already thought about once. A stack trace filter should be configurable either globally or for specific writers. To do this, a stack trace filter must not change an existing exception (or other throwable), but return a new one.

interface StackTraceFilter {

	Throwable filter(Throwable ex);

}

Like policies, stack filters should be extendable as META-INF services. In the standard set of tinylog I see the following four stack trace filters:

  1. keep: a|b for keeping only stack trace elements with the package a and b and their sub packages.

  2. remove: a|b for removing all stack trace elements with the package a and b and their sub packages.

  3. discard-cause for removing all cause throwables.

  4. compress-cause for removing stack trace elements from a parent throwable which are just repeated from cause throwables like JBoss does it by default. I love this feature when working with JBoss application servers!

These filters should be combinable, like for example (in tinylog 2 properties are not starting anymore with tinylog.):

stacktrace = keep: org.mypackage|com.mypackage, remove: org.mypackage.test
writer1.stacktrace = discard-cause
writer1.stacktrace = compress-cause

Criticism and suggestions for improvement are welcome!

@pmwmedia pmwmedia added this to the 2.1 milestone Sep 18, 2018
@pmwmedia pmwmedia self-assigned this Sep 6, 2019
@pmwmedia
Copy link
Member

pmwmedia commented Jan 6, 2020

Fixed in branch v2.1

@pmwmedia pmwmedia closed this as completed Jan 6, 2020
@github-actions
Copy link

github-actions bot commented Oct 9, 2022

This closed issue has been locked automatically. However, please feel free to file a new issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

2 participants