I'm working on building an Android app and I'm wondering what the best approach is to debugging like that of console.log in javascript
The Log class:
API for sending log output.
Generally, use the Log.v() Log.d() Log.i() Log.w() and Log.e()
methods.
The order in terms of verbosity, from least to most is ERROR, WARN,
INFO, DEBUG, VERBOSE. Verbose should never be compiled into an
application except during development. Debug logs are compiled in but
stripped at runtime. Error, warning and info logs are always kept.
Outside of Android, System.out.println(String msg) is used.
Use the Android logging utility.
http://developer.android.com/reference/android/util/Log.html
Log has a bunch of static methods for accessing the different log levels. The common thread is that they always accept at least a tag and a log message.
Tags are a way of filtering output in your log messages. You can use them to wade through the thousands of log messages you'll see and find the ones you're specifically looking for.
You use the Log functions in Android by accessing the Log.x objects (where the x method is the log level). For example:
Log.d("MyTagGoesHere", "This is my log message at the debug level here");
Log.e("MyTagGoesHere", "This is my log message at the error level here");
I usually make it a point to make the tag my class name so I know where the log message was generated too. Saves a lot of time later on in the game.
You can see your log messages using the logcat tool for android:
adb logcat
Or by opening the eclipse Logcat view by going to the menu bar
Window->Show View->Other then select the Android menu and the LogCat view
console.log() in java is System.out.println(); to put text on the next line
And System.out.print(); puts text on the same line.
public class Console {
public static void Log(Object obj){
System.out.println(obj);
}
}
to call and use as JavaScript just do this:
Console.Log (Object)
I think that's what you mean
Related
First of all, Java is not my usual language, so I'm quite basic at it. I need to use it for this particular project, so please be patient, and if I have omitted any relevant information, please ask for it, I will be happy to provide it.
I have been able to implement coreNLP, and, seemingly, have it working right, but is generating lots of messages like:
ene 20, 2017 10:38:42 AM edu.stanford.nlp.process.PTBLexer next
ADVERTENCIA: Untokenizable: 【 (U+3010, decimal: 12304)
After some research (documentation, google, other threads here), I think (sorry, I don't know how I can tell for sure) coreNLP is finding the slf4j-api.jar in my classpath, and logging through it.
Which properties of the JVM can I use to set logging level of the messages that will be printed out?
Also, in which .properties file I could set them? (I already have a commons-logging.properties, a simplelog.properties and a StanfordCoreNLP.properties in my project's resource folder to set properties for other packages).
Om’s answer is good, but two other possibly useful approaches:
If it is just these warnings from the tokenizer that are annoying you, you can (in code or in StanfordCoreNLP.properties) set a property so they disappear: props.setProperty("tokenize.options", "untokenizable=NoneKeep");.
If slf4j is on the classpath, then, by default, our own Redwoods logger will indeed log through slf4j. So, you can also set the logging level using slf4j.
If I understand your problem, you want to disable all StanfordNLP logging message while the program is executing.
You can disable the logging message. Redwood logging framework is used as logging framework in Stanford NLP. First, clear the Redwood's default configuration(to display log message) then create StanfordNLP pipeline.
import edu.stanford.nlp.util.logging.RedwoodConfiguration;
RedwoodConfiguration.current().clear().apply();
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Hope it helps.
In accordance with Christopher Manning's suggestion, I followed this link
How to configure slf4j-simple
I created a file src/simplelogger.properties with the line org.slf4j.simpleLogger.defaultLogLevel=warn.
I am able to solve it by setting a blank output stream to system error stream.
System.setErr(new PrintStream(new BlankOutputStream())); // set blank error stream
// ... Add annotators ...
System.setErr(System.err); // Reset to default
Accompanying class is
public class BlankOutputStream extends OutputStream {
#Override
public void write(int b) throws IOException {
// Do nothing
}
}
Om's answer disables all logging. However, if you wish to still log errors then use:
RedwoodConfiguration.errorLevel().apply();
I also use jdk logging instead of slf4j logging to avoid loading slfj dependencies as follows:
RedwoodConfiguration.javaUtilLogging().apply();
Both options can be used together and in any order. Required import is:
import edu.stanford.nlp.util.logging.RedwoodConfiguration;
I have the following example code :
import java.util.logging.Logger;
public class LoggingExample {
private static final Logger LOGGER = Logger.getLogger(
Thread.currentThread().getStackTrace()[0].getClassName() );
public static void main(String[] args) {
LOGGER.setLevel(Level.ALL);
LOGGER.info("Logging an INFO-level message");
LOGGER.fine("Logging an INFO-level message2");
}
}
With the output to console of:
05/06/2014 12:07:09 ztesting.Loger.LoggingExample main
INFO: Logging an INFO-level message
And I have several question:
The entire output is in red, can I set the output to be in different colors according to the level?
Can I not display the first line "05/06/2014 12:07:09 ztesting.Loger.LoggingExample main"
Can I block the output to console completely, meaning I would only set output to a log file.
Not as important, but why does the fine row does not display ?
P.S. I m using netbeans 6.9
ALSO tried using the following links and it got me no where
Is there any way to remove the information line from java.util.logging.Logger output?
How do I get java logging output to appear on a single line?
Thanks for any help ..
Most of your questions are answered in different tutorials available for java.util.logging. One example here
You should also read the javadoc.
"Can I not display the first line "05/06/2014 12:07:09 ztesting.Loger.LoggingExample main"
Yes, using Formatters
"Can I block the output to console completely, meaning I would only set output to a log file."
Yes, set the log level, filter or handler in a configuration file
"The entire output is in red, can I set the output to be in different colors according to the level?"
This is specific to Netbeans which I don't personally use so I can't answer that.
Personally I prefer SLF4J with Logback over JUL...
PlatformLogUtil.logAsError(Activator.getDefault(), new Status(IStatus.ERROR, "com.sample.example",enter code here "ERROR"));
I am using above code for Logging in eclipse problems log.
But it is not visible in problems log but able to see in console.
Can any one suggest is it right what i am performing in above code or do i need to do some thing else to view in Problem Log in eclipse.
If you take a look at the PlatformLogUtil implementation, you will see, that it does not create any problems, it just logs the error, which is shown in the "Error Log" view.
In order to show a problem in the problem view, you need to create a marker for your problem. Please read "Mark My Words" article to get more information on how to do it.
void reportError(IResource resource, int line, String msg) {
IMarker m = resource.createMarker(IMarker.PROBLEM);
m.setAttribute(IMarker.LINE_NUMBER, line);
m.setAttribute(IMarker.MESSAGE, msg);
m.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
m.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
}
This will log the error in the .log file in the workspace .metadata directory. The Error Log view should also show the error.
If the plugin you pass to PlatformLogUtil is null (from Activator.getDefault()) then the error is sent to the console.
My Program not error is perfect work but I feel annoyed from warning message.
So, I want to hide it from program console. What should I do?
(i can't edit html source code)
[Thread-4] WARN jodd.lagarto.dom.LagartoDOMBuilderTagVisitor - Orphan closed tag ignored </meta>
[Thread-3] WARN jodd.lagarto.dom.LagartoDOMBuilderTagVisitor - Unclosed tag closed: <p>
Thanks for kindness.
Oh Yea!
I can find solution with myself. (Thank a lot Elliott Frisch for guide :) )
i use Logback Library
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;
public class Main {
final static Logger joddlogger = (Logger) LoggerFactory.getLogger("jodd");
public static void main(String[] argv) {
joddlogger.setLevel(ch.qos.logback.classic.Level.OFF);
//do something...
}
}
That is one correct answer, as #Jaynova and #Elliott Frisch said. Jodd uses Logback, so all you have to do is to mute the logging category in logback configuration, as explained in the other answer. You can do that from Java or from logback configuration file.
There is one more solution, added recently: LagartoDOMBuilder has a property parsingErrorLogLevel, which purpose is to set the log level of parsing errors (by default is set to WARN). Sometimes, its important to have these errors in the log (eg parsing your own code), and sometimes its just annoying (parsing live code, usually with lot of errors).
So while you can switch off the all logging for that package, you may also mute just the parsing errors by setting the level to DEBUG (assuming your global log level is higher).
So, in short, get the LagartoDOMBuilder instance before parsing and set this flag :)
Some logging levels appear to be broke?
I run a Java web start (which I will begin to call JWS from now on) application straight from a GlassFish 3.1.2.2 instance. The client has a static logger like so:
private final static Logger LOGGER;
static {
LOGGER = Logger.getLogger(App.class.getName());
// Not sure how one externalize this setting or even if we want to:
LOGGER.setLevel(Level.FINER);
}
In the main method, I begin my logic with some simple testing of the logging feature:
alert("isLoggable() INFO? " + LOGGER.isLoggable(Level.INFO)); // Prints TRUE!
alert("isLoggable() FINE? " + LOGGER.isLoggable(Level.FINE)); // ..TRUE
alert("isLoggable() FINER? " + LOGGER.isLoggable(Level.FINER)); // ..TRUE
alert("isLoggable() FINEST? " + LOGGER.isLoggable(Level.FINEST)); // ..FALSE
My alert methods will display a JOptionPane dialog box for "true GUI logging". Anyways, you see the printouts in my comments I added to the code snippet. As expected, the logger is enabled for levels INFO, FINE and FINER but not FINEST.
After my alert methods, I type:
// Info
LOGGER.info("Level.INFO");
LOGGER.log(Level.INFO, "Level.INFO");
// Fine
LOGGER.fine("Level.FINE");
LOGGER.log(Level.FINE, "Level.FINE");
// Finer
LOGGER.finer("Level.FINER");
LOGGER.log(Level.FINER, "Level.FINER");
LOGGER.entering("", "Level.FINER", args); // <-- Uses Level.FINER!
// Finest
LOGGER.finest("Level.FINEST");
LOGGER.log(Level.FINEST, "Level.FINEST");
I go to my Java console and click on the tab "Advanced", then I tick "Enable logging". Okay let's start the application. Guess what happens? Only Level.INFO prints! Here's my proof (look at the bottom):
I've done my best to google for log files on my computer and see if not Level.FINE and Level.FINER end up somewhere on the file system. However, I cannot find the log messages anywhere.
Summary of Questions
Why does it appear that logging of Level.FINE and Level.FINER does not work in the example provided?
I set the logging level in my static initializing block, but I'd sure like to externalize this setting to a configuration file of some sort, perhaps packaged together with the EAR file I deploy on GlassFish. Or why not manually write in some property in the JNLP file we download from the server. Is this possible somehow?
Solution for problem no 1.
After doing a little bit more reading on the topic, I concluded that a logger in Java uses a handler to publish his logs. And this handler in his turn has his own set of "walls" for what levels he handles. But this handler need not be attached directly to our logger! You see loggers are organized in a hierarchical namespace and a child logger may inherit his parents handlers. If so, then By default a Logger will log any output messages to its parent's handlers, and so on recursively up the tree (see Java Logging Overview - Oracle).
I ain't saying I get the full picture just yet, and I sure didn't find any quotes about how all of this relates to a Java Web Start application. Surely there has to be some differences. Anyways, I did manage to write together this static initializing block that solves my immediate problem:
static {
LOGGER = Logger.getLogger(App.class.getName());
/*
* This logic can be externalized. See the next solution!
*/
// DEPRECATED: LOGGER.setLevel(Level.FINER);
if (LOGGER.getUseParentHandlers())
LOGGER.getParent().getHandlers()[0].setLevel(Level.FINER);
else
LOGGER.setLevel(Level.FINER);
}
Solution for problem no 2.
The LogManager API docs provided much needed information for the following solution. In a subdirectory of your JRE installation, there is a subdirectory called "lib" and in there you shall find a "logging.properties" file. This is the full path to my file on my Windows machine:
C:\Program Files (x86)\Java\jre7\lib\logging.properties
In here you can change a lot of flavors. One cool thing you could do is to change the global logging level. In my file, this was done on row 29 (why do we see only a dot in front of the "level"? The root-parent of all loggers is called ""!). That will produce a hole lot of output; on my machine I received about one thousand log messages per second. Thus changing the global level isn't even plausible enough to be considered an option. Instead, add a new row where you specify the level of your logger. In my case, I added this row:
martinandersson.com.malivechat.app.App.level = FINER
However, chances are you still won't see any results. In solution no 1, I talked about how loggers are connected to handlers. The default handler is specified in logging.properties, most likely on row 18. Here's how my line reads:
handlers= java.util.logging.ConsoleHandler
Also previously, I talked about how these handlers in their turn use levels for what should trouble their mind. So, find the line that reads something like this (should now be on row 44?):
java.util.logging.ConsoleHandler.level = INFO
..and in my case I swapped "INFO" to "FINER". Problem solved.
But!
My original inquiry into this matter still hasn't provided an answer how one can set these properties closer in par with the application deployment. More specifically, I would like to attach these properties in a separate file, bundled with the application EAR file I deploy on GlassFish or something like that. Do you have more information? Please share!