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

Capture JUL logging & Add Java HTTP Server plugin #246

Closed
albertus82 opened this issue Jul 25, 2017 · 10 comments
Closed

Capture JUL logging & Add Java HTTP Server plugin #246

albertus82 opened this issue Jul 25, 2017 · 10 comments
Milestone

Comments

@albertus82
Copy link
Contributor

Dear Trask,
I added this aspect to the Logger Plugin in order to catch java.util.logging log records; I referenced it in glowroot.plugin.json but I don't see any Error in Glowroot's Errors tab even if I log at SEVERE level. Can you tell me if I'm missing something? I'm monitoring a simple Java client application (jar), not a web app. Thank you a lot.

Alberto

@trask
Copy link
Member

trask commented Jul 27, 2017

Hi @albertus82, thanks for posting! Are you logging an exception at SEVERE, or only a message? By default it will only mark the trace as error when an error is logged with an exception (see logic in https://github.com/glowroot/glowroot/blob/v0.9.22/agent/plugins/logger-plugin/src/main/java/org/glowroot/agent/plugin/logger/LoggerPlugin.java#L43)

Check out the logger configuration https://demo.glowroot.org/config/plugin?plugin-id=logger. I think maybe the default configuration is not the best, and maybe should change https://github.com/glowroot/glowroot/blob/v0.9.22/agent/plugins/logger-plugin/src/main/resources/META-INF/glowroot.plugin.json#L16.

By the way, java.util.logging support would be a great addition. Let me know if you plan to send PR, otherwise I will plan to add at some point.

Thanks,
Trask

@albertus82
Copy link
Contributor Author

albertus82 commented Jul 27, 2017

Hi @trask, thank you for your reply. Let's start clean:

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GlowrootJulTest {

	private static final int RUNNING_TIME_SECS = 300;

	private static final Logger logger = Logger.getLogger(GlowrootJulTest.class.getName());

	public static void main(final String... args) throws InterruptedException {
		System.out.println("Starting " + GlowrootJulTest.class.getName() + "...");
		for (int i = 0; i < RUNNING_TIME_SECS; i++) {
			TimeUnit.SECONDS.sleep(1);
			logger.log(i % 2 == 0 ? Level.SEVERE : Level.WARNING, "log message #" + i, new IllegalStateException("exception message #" + i));
		}
		System.out.println("Terminating " + GlowrootJulTest.class.getName() + '.');
	}
}

Running this code with the javaagent that includes my additions, I see no errors logged in Glowroot, but probably I'm missing something because if I run the same code using Log4j, I still have no errors in Glowroot:

import java.util.concurrent.TimeUnit;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class GlowrootLog4jTest {

	private static final int RUNNING_TIME_SECS = 300;

	private static final Logger logger = Logger.getLogger(GlowrootLog4jTest.class.getName());

	public static void main(final String... args) throws InterruptedException {
		System.out.println("Starting " + GlowrootLog4jTest.class.getName() + "...");
		for (int i = 0; i < RUNNING_TIME_SECS; i++) {
			TimeUnit.SECONDS.sleep(1);
			logger.log(i % 2 == 0 ? Level.ERROR : Level.WARN, "log message #" + i, new IllegalStateException("exception message #" + i));
		}
		System.out.println("Terminating " + GlowrootLog4jTest.class.getName() + '.');
	}

}

log4j.properties:

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

I'm sorry I'm not able to detect what's wrong.

Anyway I'll be glad to make a pull request when this thing will work properly, of course. Thank you again.

@trask
Copy link
Member

trask commented Jul 27, 2017

Ah, I see, you don't have any transactions defined.

Try this:

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GlowrootJulTest {

    private static final int RUNNING_TIME_SECS = 300;

    private static final Logger logger = Logger.getLogger(GlowrootJulTest.class.getName());

    public static void main(final String... args) throws InterruptedException {
        System.out.println("Starting " + GlowrootJulTest.class.getName() + "...");
        for (int i = 0; i < RUNNING_TIME_SECS; i++) {
            sleepAndLog(i);
        }
        System.out.println("Terminating " + GlowrootJulTest.class.getName() + '.');
    }

    private static void sleepAndLog(int i) throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        logger.log(i % 2 == 0 ? Level.SEVERE : Level.WARNING, "log message #" + i,
                new IllegalStateException("exception message #" + i));
    }
}

and import this instrumentation through the UI (under Configuration > Instrumentation):

  {
    "className": "GlowrootJulTest",
    "methodName": "sleepAndLog",
    "methodParameterTypes": [
      "int"
    ],
    "captureKind": "transaction",
    "transactionType": "Test",
    "transactionNameTemplate": "Test",
    "traceEntryMessageTemplate": "test",
    "timerName": "test"
  }

This will define a transaction for each call to sleepAndLog(int), and you will hopefully start seeing the errors in Glowroot.

@albertus82
Copy link
Contributor Author

albertus82 commented Jul 27, 2017

Thank you @trask, it works! I'm now refining the work. Can I assume that java.util.logging package is present in Glowroot classpath? I think that use @Shim in this case may be overkill and can make log message formatting complicated.

@trask
Copy link
Member

trask commented Jul 27, 2017

Great. Yes, good point, you can assume java.util.logging is available and avoid the @Shim.

@albertus82
Copy link
Contributor Author

albertus82 commented Jul 27, 2017

@trask I'm experiencing troubles running the test, maybe a classloader issue? The advice doesn't run, but it works properly outside the test environment. Any ideas? TY

Tests run: 6, Failures: 6, Errors: 0, Skipped: 0, Time elapsed: 9.149 sec <<< FAILURE! - in org.glowroot.agent.plugin.logger.JavaLoggingIT
testLog(org.glowroot.agent.plugin.logger.JavaLoggingIT) Time elapsed: 0.393 sec <<< FAILURE!
org.junit.ComparisonFailure: expected:<"[efg]"> but was:<"[]">
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at org.glowroot.agent.plugin.logger.JavaLoggingIT.testLog(JavaLoggingIT.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:367)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:274)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:161)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)

@albertus82
Copy link
Contributor Author

albertus82 commented Jul 29, 2017

Hi @trask, solved: the tests run fine but, like LogbackIT, work only when shaded, so I added an assume in the test's setUp() method. Travis CI builds almost fine, I'm addressing the last issues.

I'm going to open a PR containing this improvement as well as a new plugin named Java HTTP Server Plugin that I wrote to support the Java HTTP Server bundled with JRE since version 6. Tests are included.
This new plugin is similar to the Servlet Plugin but it's simpler because Java HTTP Server doesn't have native support for sessions, async requests and parameters. I used that server in a standalone application (RouterLogger) that I wrote for monitoring purposes, so I'm already using the plugin on a real scenario. I hope you appreciate it. Of course, feel free to rename the plugin if you don't like the name I chose; I took the name from the title of the Oracle documentation page.

Thank you a lot for your great work.

@trask
Copy link
Member

trask commented Aug 2, 2017

Closed by #248

@trask trask closed this as completed Aug 2, 2017
@trask trask changed the title java.util.logging support Capture JUL logging & Add Java HTTP Server plugin Dec 12, 2017
@trask trask added this to the v0.9.23 milestone Dec 12, 2017
@godhanairi
Copy link

Hi,
I have integrated glow root on my system which is running but I don't understand how I attach my java application in glow root for monitoring?

@trask
Copy link
Member

trask commented Feb 2, 2018

Hi @godhanairi, have you followed instructions at https://github.com/glowroot/glowroot/wiki/Agent-Installation? Can you provide more details about what you've tried and where you are stuck?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants