Java console logs windows

Java logs to console or file – log4j2 or logback with slf4j

In this post, we will look into the Logback library and how we can utilise to log messages to console and also in into a file in java projects.

You might be aware of one library log4j and used as well, now question is why logback is preferred over log4j, follow here for details.

You can use any of the java logging libraries log4j2 or logback with out changing the code. you just need to change the dependencies into pom.xml. this is possible by another intermediate library SLF4J.

both log4j2 or logback supports slf4j natively, so you need not to worry about if you use any of them.

Libraries required in pom.xml

If you want to use log4j2

 org.apache.logging.log4j log4j-api 2.14.1  org.apache.logging.log4j log4j-core 2.14.1  org.apache.logging.log4j log4j-slf4j-impl 2.14.1 

If you are using logback

  ch.qos.logback logback-classic 1.2.6 test 

Let’s say you are using logback library in your project –

We need a xml file with name “logback.xml”, and should be placed under

current project folder | src | main | resources folder

    UTF-8 %d %-4relative [%thread] %-5level %logger - %msg%n     

place this above xml to print messages to the console.

Code implementation –

Create a java file – Log.java

package baseClasses; import org.slf4j.LoggerFactory.getLogger; import org.slf4j.Logger; import static java.lang.invoke.MethodHandles.lookup; public class Log < public static void main(String args[]) < final Logger logger = getLogger(lookup().lookupClass()); logger.info("this is info message"); logger.error("this is error message"); >>
2018-12-04 13:02:28,904 223 [main] INFO logback.Logback - this is info message 2018-12-04 13:02:28,906 225 [main] ERROR logback.Logback - this is error message

Like info / error, we have few more methods or we call levels to print messages, those are debug, trace and warn [Find more details – find class Logger here]

Explanation –

The output is basically in the pattern as mentioned in the Pattern attribute of logback.xml

%d %-4relative [%thread] %-5level %logger — %msg%n

Читайте также:  Threading python что это

logger -> class name from where message is sent

msg -> actual message written inside the methods like info() / error()

Till now we have seen how to print the messages to console, we will see how to print the messages into console and file as well

Code used inside the Logback class as mentioned above will remain same, only we need to update the logback.xml

    UTF-8 %d %-4relative [%thread] %-5level %logger - %msg%n    $/logs/app-$.log logs/app-%i.log.gz 1 2  2MB  UTF-8 %d %-4relative [%thread] %-5level %logger - %msg%n      

If you observe the above logback.xml content, we have added another appender as name = file, and customised the file name with respect to the “timeStamp” tag and variable.

Now run the Logback.java file, observe that there is a new folder generated as logs under your current project and the message sent is printed on console and also in to the log file.

Note – If file not created, then right click on the project -> Refresh

Источник

22 Java Console, Tracing, and Logging

This topic describes the Java Console, a debugging aid that redirects any System.out and System.err to the console window. The console is available for applets running with Java Plug-in and applications running with Java Web Start.

This topic includes the following sections:

22.1 Debugging Options in the Java Console

Figure 22-1 shows the options that are available in the Java Console for debugging applets and Java Web Start applications.

Figure 22-1 Java Console

An option is selected by typing its letter or number when the Java Console window has focus. The options are described in Table 22-1.

Table 22-1 Java Console Debugging Options

Clears the Java Console window.

Triggers finalization on the objects in the finalization queue and then displays memory information. Memory refers to the current heap size used by the JRE. Free is the available memory that is free in the heap. The percent (xx%) is the free memory as a percent of the total heap size.

Triggers garbage collection and displays memory information as described above.

Displays help message, which is being described here.

Displays a list of the cached ClassLoader objects in the Java Plug-in. Classes are cached to avoid having to load them again when returning to previously-visited pages. When a page is visited the first time, a ClassLoader object will be created and all of the classes that are downloaded will be cached in that object. These objects are created and cached according to their codebase . To identify a ClassLoader object, the «classloader list» displays the codebase for that object. Additional information displayed with a ClassLoader object includes zombie , cache and info . zombie = true indicates that a ClassLoader object is not being used (i.e., the applet is not currently loaded on the page). cache = true indicates that the applet should be cached, while false indicates that the applet will be destroyed when the page is left. info is a value used for debugging.

Читайте также:  How to check errors in html

Displays heap memory usage as described above.

Triggers logging, which directs output from the Java Plug-in Console to a log file.

Reloads the proxy configuration.

Causes the Java Console to disappear from the main screen.

Reloads the policy configuration.

Prints out the system properties. This is mostly for debugging.

Prints out all the existing thread groups. The first group shown is Group main . ac stands for active count; it is the total number of active threads in a thread group and its child thread groups. agc stands for active group count; it is the number of active child thread groups of a thread group. pri stands for priority; it is the priority of a thread group. Following Group main , other thread groups will be shown as Group < name > , where name is the URL associated with an applet. Individual listings of threads will show the thread name, the thread priority, alive if the thread is alive or destroyed if the thread is in the process of being destroyed, and daemon if the thread is a daemon thread.

This removes (destroys) all ClassLoader objects in the cache.

Modified jar files will be downloaded from the server when a page with an applet is refreshed or revisited if you first do this: type «x» in the Java Console to clear the Classloader cache.

22.2 Tracing and Logging

Tracing is a facility to redirect any output in the Java Console to a trace file. Similar to tracing, logging is a facility to redirect any output in the Java Console to a log file using the Java Logging API.

Читайте также:  Java class with database connection

22.2.1 Tracing

Tracing for Java Plug-in and Java Web Start can be turned on by setting the property deployment.trace property to true . This property turns on all tracing facilities inside Java Plug-in and Java Web Start. To enable more fine-grained tracing, the deployment.trace.level property can be used. The deployment.trace.level property can have one of the following values:

To use all of the tracing levels, set deployment.trace.level to all .

To enable tracing while running, you can set trace-level options (0-5) in the Java Console, with the following meanings:

  • 0: off
  • 1: basic
  • 2: network, cache, and basic
  • 3: security, network and basic
  • 4: extension, security, network and basic
  • 5: LiveConnect, extension, security, network, temp, basic, and Deployment Rule Set

Another way to set fine-grained tracing is through the Java Control Panel. For example, to enable tracing for everything (option 5 above), enter the following options in the «Java Run Time Parameters» textfield:

-Ddeployment.trace=true -Ddeployment.trace.level=all

Tracing set through the Control Panel takes effect when Java Plug-in or Java Web Start is launched, but changes made through the Control Panel while Java Plug-in or Java Web Start is running has no effect until restarted.

22.2.2 Logging

To enable logging perform the following actions:

  • Open Java Control Panel
  • Click Advanced tab.
  • Select Enable Logging under the Debugging option

22.2.3 File Names

  • plugin.trace — Name of the trace file for Java Plug-in.
  • plugin.log — Name of the log file for Java Plug-in.
  • javaws.trace — Name of the trace file for Java Web Start.
  • javaws.log — Name of the log file for Java Web Start.
  • jcp.trace — Name of the trace file that is created when the Java Control Panel is run.

is a number that is generated when the file is created.

22.2.4 File Locations

The default location (directory) of the trace and log files is:

  • /.java/deployment/log on Solaris and Linux
  • ~/Library/Application Support/Oracle/Java/Deployment/log on OS X
  • \Sun\Java\Deployment\log on Windows

If the environment variable USER_JPI_PROFILE is set to then the trace and log files will be written to:

  • /.java/deployment/log on Solaris and Linux
  • /Library/Application Support/Oracle/Java/Deployment/log on OS X
  • \Sun\Java\Deployment\log on Windows

Источник

Оцените статью