In a large project we have something like:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
class Test {
static final Log logger = LogFactory.getLog(Test.class);
public void execute() {
log.info("writing some info");
log.error("writing some error");
}
}
I know that as it runs:
I am using
class org.apache.commons.logging.impl.Log4JLogger
Because I am able to connect remotely and put a breakpoint on my debug and see what class it is.
how can I figure out what log configuration is using ?
where is it picked up from ?
how is it configured ?
p.s.
Note the app. is actually quite complex and it's running within web logic app server. I don't have access to all its java code.
p.s.II
My idea would be to inject some code inside the execute method to determine where the heck log4j.properties is , if it is there at all and so on...
Related
I am beginner of java
I am making some program with excel(poi)
and I've been trying to use log4j2
to log what is wrong when run jar or exe
so I got a question
when I searched how to use log4j2 on internet
there is only a usage which like
try{some method}
catch(exception ex)
{logger.catching(ex)}
is it the only way to log ?
is there a way to log without using try catch?
for now ,I think if I use try and catch
I need to use a lot of try catch or throws..
thank you in advance!
Sure. You can invoke logger.whatever() anywhere. E.g. logger.info(); Method catching() is used to log an exception or error that has been caught. That's why in your example it's used with try-catch block. Read more in docs.
Yes, you can log things other than exceptions. In fact you can log anything you want. Please see the log4j2 manual, in particular the page called Java API
You simply create your logger and invoke one of the methods specific to the level you want for your event, or if you're using a custom level use the log method. See the architecture page of the manual for more on log levels.
The code below is from the Java API page of the manual and shows you how to log the message "Hello, World!" at INFO level.
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class HelloWorld {
private static final Logger logger = LogManager.getLogger("HelloWorld");
public static void main(String[] args) {
logger.info("Hello, World!");
}
}
Edited to restart question from scratch due to complaints. I am a newbie to this format and to intellij so please excuse...
I am building a project in intellij for class. This project imports jnetcap and uses it to process a captured pcap file. My issue is I have two class files I am trying to integrate. NetTraffic which is the user interface class, and ProcessPacket that actually reads in the packet and does the work.
I have tried to make a project and import ProcessPacket into NetPacket but have been unsuccessful so far. I am sure I am missing something simple in this process but I just can not find anything showing the proper way to do this.
I have gotten it working by making a package under the src directory and adding both files to that package. This doesn't require an import from the NetPacket class and seems to work but my worry is that I need to be able to run this from a linux command line. I have been working all semester so far with everything in one source file so it hasn't been an issue until now. I don't remember using packages in the past under eclipse to do this.
Can someone offer a step by step process on how to properly add these source files to my project so that I am able to import ProcessPacket into NetTraffic or will leaving like this in a package work fine?
The files in question reside in package named nettraffic in src directory.
NetTraffic.java
package nettraffic;
public class NetTraffic {
public static ProcessPacket pp;
public static void main (String args[]) {
pp = new ProcessPacket();
pp.PrintOut();
}
}
ProcessPacket.java
package nettraffic;
import org.jnetpcap.*;
public class ProcessPacket {
public ProcessPacket() {
}
public void PrintOut() {
System.out.println("Test");
}
}
Note there is no real functionality in these at this time. Just trying to get the class import syntax correct before continuing. Again while this seems to work as a package I want to have it done without using a package and importing ProcessPacket.java into NetTraffic.java.
public class NetTraffic {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
You're calling the PrintOut() method outside of any constructor or method or similar block (static or non-static initializer blocks...), and this isn't legal. Put it in a constructor or method.
public class NetTraffic {
public NetTraffic() {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
}
I try to add a new custom command to the spring boot remote shell without success.
In the documentation is only a groovy example available but I like to use Java do create a new command.
http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-remote-shell.html
I also check the CRaSH documentation: http://www.crashub.org/1.3/reference.html#_java_commands
I put my class under package commands and crash.commands but I don't see my new command if I connect to the shell via ssh and type help. Any idea what I'm doing wrong?
Here is my Java Code:
package commands;
import org.crsh.cli.Command;
import org.crsh.cli.Usage;
import org.crsh.command.BaseCommand;
import org.springframework.stereotype.Component;
#Component
public class hello extends BaseCommand {
#Command
#Usage("Say Hello")
public String test() {
return "HELLO";
}
}
If your class is called 'hello', put it in
resources/crash/commands/hello/hello.java
Having a package statement doesn't matter.
You don't have to put anything inside src, just have this inside resources.
#Component isn't required.
This class is compiled during runtime. See:
org.crsh.lang.impl.java.JavaCompiler#compileCommand
Therefor any dependency injection requirements need to be considered accordingly with lazy initialization.
Put the class in the default package (i.e. omit the package statement) and put the Java source for the class in src/main/resources/commands. Spring/Crash will compile the Java code the first time you invoke the command. Also, if the class implements a single command the method name should be ‘main’ otherwise users have to type:
> hello test
You also don't need the #Component annotation on the class as it is not a Spring managed bean.
I'm building a simple app in IntelliJ IDEA 13 and can't seem to figure out how to get log4j working. My app is a simple demo that I made to make sure I could build something functional, all it does is multiply two random numbers and uses apache tomcat to put it on a localhost that I can access via my browser.
Here is the class code:
package Sample;
log4j-api-2.0.jar;
log4j-core-2.0.jar;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
public class HelloWorld {
public static double getMessage() {
return Math.random()* Math.random();
}
private static Logger log = LogManager.getRootLogger();
log.debug("Debugging Message");
log.info("Informational message");
log.warn("Warning Message");
System.in.read();
}
I'm getting the error "class or interface expected" at the import lines and jar file lines so I don't think I've placed the corresponding files in the right directory. That's also causing the rest of the logging lines (from private static Logger... on) to generate errors.
1. The following isn't valid Java:
log4j-api-2.0.jar;
log4j-core-2.0.jar;
You only need the import lines, e.g.,
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
2. The .jar files must be associated with your project.
You can:
Right-click the "External Libraries" section and add them that way, or...
Use Maven and add them as project dependencies, or...
Use some other dependency management and/or build tool, e.g., Ant + Ivy, Gradle, etc.
3. You need to move the logging statements into a place where code is valid, like in a method:
package sample;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
public class HelloWorld {
private static final Logger log = LogManager.getRootLogger();
public static void main(String[] args) {
log.debug("Debugging Message");
log.info("Informational message");
log.warn("Warning Message");
}
}
Okay, this had been making me very mad. I've followed almost 8 tutorials all over the Internet and in the end, I got my Red5 server instance working. Good for me! But when I'm calling my Java methods in my Red5 apps from my AS3 apps, in the 'Console' window in Eclipse, I got this error :
[ERROR] [NioProcessor-1] org.red5.server.service.ServiceInvoker - Method getTheName with parameters [] not found in org.red5.core.Application#17e5fde
Here's my Application.java file.
package org.red5.core;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.service.ServiceUtils;
/**
* Sample application that uses the client manager.
*
* #author The Red5 Project (red5#osflash.org)
*/
public class Application extends ApplicationAdapter {
/** {#inheritDoc} */
#Override
public boolean connect(IConnection conn, IScope scope, Object[] params) {
return true;
}
/** {#inheritDoc} */
#Override
public void disconnect(IConnection conn, IScope scope) {
super.disconnect(conn, scope);
}
public String getTheName() { return "MyName!"; }
}
And here's my AS3 code. I just put this on the Timeline.
var nc:NetConnection = new NetConnection();
nc.connect("http://localhost/Mintium/RoomHere", "SomeUsernameHere");
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
nc.objectEncoding = ObjectEncoding.AMF0;
function onNetStatus(e:NetStatusEvent):void
{
switch (e.info.code)
{
case "NetConnection.Connect.Success" :
trace("connected");
nc.call("getTheName", new Responder(getName_result, getName_error));
break;
}
}
function getName_result(res:Object):void { append("Name : " + res.toString()); }
function getName_error(res:Object):void { append(res.toString()); }
Its been a week I've been trying to figure it out and my dateline is next month. If this stuff is not solved, I'm gonna fail my assessment. Please help me with my problems. Thank you very much.
Sorry I did not see this 2 months ago, I could have helped you pass your assessment. Nevertheless, I think I can answer this question, having had a similar problem calling Red5 services.
The key to solving this problem is in those parts of Red5 that utilize the Spring Framework. In your project, there should be a file called red5-web.xml that resides in the Server project's WEB-INF folder. This file contains some Bean dependencies used by Red5's Spring components. This is not mentioned in the tutorials that I read, or even in most of the (rather sparse and distributed) red5 programming documentation.
What you have to do is add a bean entry for your method in that file. In your case, the entry should look like this:
<bean id="getTheName.service" class="org.red5.core.Application" />
Note my use of the name of your function, with ".service" appended. I do not understand why, but you need the ".service" appended in order for Red5 to find your function. You need to add a similar entry for every class whose functions you want to use as services.
Of course, I based everything I said above on the fact that you put the service into the Application class -- something which I never do. if you read the red5-web.xml file, you will see that there is already an entry for that class, because it is already injected through Spring as the class that acts as an "endpoint" for processing requests over the web. I do not know if using the Application class as an endpoint and a provider of services is a good idea (it violates "separation of concerns" in OOP and may cause problems with Spring).
What I usually do is add a separate class in the org.red5.core package (or any other package you might want) that acts to deliver the desired service, then put an entry into red5-web.xml that injects the class and its method. So, for your project, lets assume you have a class called NameProvider in the org.red5.core package:
public class NameProvider
{
public NameProvider() {}
public String getTheName() { return("MyName!"); }
}
then you add the following entry to your red5-web.xml file:
<bean id="getTheName.service" class="org.red5.core.NameProvider" />
That should make everything work.
I hope this helps you in the future, or anyone else having this problem. I just wish I'd seen this question sooner.