Comment On Bringing LOGing Back

Linda didn't have much to say about the following code snippet. In fact, she sent nothing else; therefore, I feel like I don't need to say much about it. All I know is that if I ever need a logger to produce a stack trace in Java, this is how I'll do it. [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Bringing LOGing Back

2007-04-04 09:01 • by Bob (unregistered)
WTF?
That's riduclous - noone could tink of writing that...

Re: Bringing LOGing Back

2007-04-04 09:04 • by jtwine
Java developers never cease to amaze me... :P

Re: Bringing LOGing Back

2007-04-04 09:08 • by Benanov
I think .NET does something like this when you ask for a stack trace--it'll throw an exception, catch it, and then give you the stack trace.

The tell-tale "first exception thrown" slowdown is what gave it away to us.

The .NET 'as' operator in C# works the same way. It'll try the cast and if it fails, you'll see the slowdown and get null.

Re: Bringing LOGing Back

2007-04-04 09:08 • by ethan (unregistered)
why would someone do this? I call shenanigans! this is a fake WTF!

I was hoping that wtf would post good code for April foolsand then mock anyone that can't see the "problem" =), or maybe code that looks like it doesn't work, but actually does.

Re: Bringing LOGing Back

2007-04-04 09:08 • by Welbog
At least it only catches a NullPointerException...

Re: Bringing LOGing Back

2007-04-04 09:11 • by JavaNoob (unregistered)
As a beginner in Java, I have to ask: how does this even compile?

i.doubleValue() returns a double, but if you don't assign it to something, wouldn't this fail to compile? That is, wouldn't you have to write "double j = i.doubleValue" or something?

Re: Bringing LOGing Back

2007-04-04 09:13 • by Welbog
130173 in reply to 130172
JavaNoob:
As a beginner in Java, I have to ask: how does this even compile?

i.doubleValue() returns a double, but if you don't assign it to something, wouldn't this fail to compile? That is, wouldn't you have to write "double j = i.doubleValue" or something?
It's not required to use return values in Java. I don't even think you'll get a compiler warning in this situation. At least I don't remember ever seeing such a warning.

Re: Bringing LOGing Back

2007-04-04 09:16 • by roe
Actually, this used to be the way to pull a stack trace in Java until 1.5 (where the Thread-object was equipped with similar functionality).. although the choice of exception is a bit unorthodox.

I must say though, it is quite an ingenious way to create objects without the use of the new operator...

Re: Bringing LOGing Back

2007-04-04 09:18 • by RON (unregistered)
130176 in reply to 130169
Benanov:
I think .NET does something like this when you ask for a stack trace--it'll throw an exception, catch it, and then give you the stack trace.

The tell-tale "first exception thrown" slowdown is what gave it away to us.

The .NET 'as' operator in C# works the same way. It'll try the cast and if it fails, you'll see the slowdown and get null.


Wrong. Please stop talking if you have no clue what you're talking about.

Re: Bringing LOGing Back

2007-04-04 09:18 • by Sgt. Preston (unregistered)
What's degrading about sending or receiving code in attachments?

Re: Bringing LOGing Back

2007-04-04 09:19 • by David Hall (unregistered)
130178 in reply to 130175
>> this used to be the way to pull a stack trace in Java until 1.5

nonsense. Ever since Java 0.0, you've always had the option to simply construct an exception and access its stack trace.

log.logError(new Exception())

has always worked.

Re: Bringing LOGing Back

2007-04-04 09:24 • by roe
130179 in reply to 130178
David Hall:
>> this used to be the way to pull a stack trace in Java until 1.5

nonsense. Ever since Java 0.0, you've always had the option to simply construct an exception and access its stack trace.

log.logError(new Exception())

has always worked.


That is, of course, what I meant, using exceptions to get stack traces. I was referring to the null-catch stuff as means of object-construction.

Re: Bringing LOGing Back

2007-04-04 09:26 • by Bob (unregistered)
130180 in reply to 130177
Sgt. Preston:
What's degrading about sending or receiving code in attachments?


What's malicious, contains code and usually comes in attachments?

Re: Bringing LOGing Back

2007-04-04 09:29 • by diaphanein (unregistered)
130182 in reply to 130180
Bob:
Sgt. Preston:
What's degrading about sending or receiving code in attachments?


What's malicious, contains code and usually comes in attachments?


Oooh! Oooooooh!! I know this one.... Is it pr0n?

Re: Bringing LOGing Back

2007-04-04 09:30 • by Chris (unregistered)
130183 in reply to 130180
any office document! :P

captcha: pirates (yarr there be bugs!)

Re: Bringing LOGing Back

2007-04-04 09:30 • by Chris (unregistered)
130184 in reply to 130180
Bob:
Sgt. Preston:
What's degrading about sending or receiving code in attachments?


What's malicious, contains code and usually comes in attachments?


any office document! :P

captcha: pirates (yarr there be bugs!)

Re: Bringing LOGing Back

2007-04-04 09:32 • by Gyske
I usually do something like this:

Throwable stack = new Throwable();
System.err.println("Printing the JVM stack:");
stack.printStackTrace(PrintStream out);

[edit]added optional parameter 'PrintStream out', saves you from having to redirect stderr[/edit]

Re: Bringing LOGing Back

2007-04-04 09:34 • by Bob (unregistered)
130186 in reply to 130182
diaphanein:
Bob:
Sgt. Preston:
What's degrading about sending or receiving code in attachments?


What's malicious, contains code and usually comes in attachments?


Oooh! Oooooooh!! I know this one.... Is it pr0n?


No-one ever sends me stuff like that... :-(

Re: Bringing LOGing Back

2007-04-04 09:38 • by Mystified (unregistered)
130190 in reply to 130176
RON:
Benanov:
I think .NET does something like this when you ask for a stack trace--it'll throw an exception, catch it, and then give you the stack trace.

The tell-tale "first exception thrown" slowdown is what gave it away to us.

The .NET 'as' operator in C# works the same way. It'll try the cast and if it fails, you'll see the slowdown and get null.


Wrong. Please stop talking if you have no clue what you're talking about.


Yeah, no freaking kidding. I don't even know where to start, here.

Re: Bringing LOGing Back

2007-04-04 09:48 • by Sgt. Preston (unregistered)
130193 in reply to 130180
Bob:
Sgt. Preston:
What's degrading about sending or receiving code in attachments?


What's malicious, contains code and usually comes in attachments?

That might make the practice inconvenient, but how is it "degrading"?

Re: Bringing LOGing Back

2007-04-04 09:51 • by bstorer
130194 in reply to 130182
diaphanein:
Bob:
Sgt. Preston:
What's degrading about sending or receiving code in attachments?


What's malicious, contains code and usually comes in attachments?


Oooh! Oooooooh!! I know this one.... Is it pr0n?

He said "malicious", not "delicious"!

Re: Bringing LOGing Back

2007-04-04 09:51 • by Zylon
130195 in reply to 130180
Bob:
Sgt. Preston:
What's degrading about sending or receiving code in attachments?


What's malicious, contains code and usually comes in attachments?

Executable code. Source code, however, is completely harmless, and it's generally a Really Good Idea to send code as an attachment since e-mail clients are notorious for screwing up formatting.

So I hereby nominate "As an aside, please stop sending code in attachments. It's degrading to both of us." as the designated REAL WTF of this article.

Re: Bringing LOGing Back

2007-04-04 09:57 • by Bearded Unix Guru (unregistered)
130197 in reply to 130195
Zylon:
Bob:
Sgt. Preston:
What's degrading about sending or receiving code in attachments?


What's malicious, contains code and usually comes in attachments?

Executable code. Source code, however, is completely harmless, and it's generally a Really Good Idea to send code as an attachment since e-mail clients are notorious for screwing up formatting.

So I hereby nominate "As an aside, please stop sending code in attachments. It's degrading to both of us." as the designated REAL WTF of this article.


I nominate using mail clients dumb enough to reformat whitespace as the REALLY REAL WTF. Berkeley mail FTW!

Re: Bringing LOGing Back

2007-04-04 10:01 • by piptheGeek
130198 in reply to 130169
Benanov:
I think .NET does something like this when you ask for a stack trace--it'll throw an exception, catch it, and then give you the stack trace.


This, in fact, doesn't work. I tried throwing an exception then catching it to get a stack trace. All you get is the current method. Some checking later revealed
1) A perfectly good method in diagnostics to get a stack trace from
2) That the docs do tell you that the exception stack trace only contains the amount of the stack that has been unwound while finding a catch block.

Re: Bringing LOGing Back

2007-04-04 10:02 • by Steve (unregistered)
While this isn't the most elegant way of accomplishing the task, it does seem to work as intended, at least.

The problem for the novice Java programmer, one with which I am familiar, having only taken up the language in the last couple of years after toiling in the vineyards of pure ANSI C for about two decades, is that there's just so damned much of it to assimilate at once. My copy of the O'Reilly Java in a Nutshell book for 1.5 (now outdated) is about 1200 pages, most of it description of the various standard packages. The earlier editions were at least 750 pages.

That's an awful lot of material to assimilate, especially while also trying to keep all of the object oriented concepts and semantics of the language itself straight at the same time.

Searching the web may not necessarily be helpful if you don't know the right search terms to use.

Yes, coding something like
new Throwable().printStackTrace();
is certainly nicely elegant, it isn't precisely intuitively obvious, is it?

Re: Bringing LOGing Back

2007-04-04 10:06 • by bonzombiekitty
130200 in reply to 130172
JavaNoob:
As a beginner in Java, I have to ask: how does this even compile?

i.doubleValue() returns a double, but if you don't assign it to something, wouldn't this fail to compile? That is, wouldn't you have to write "double j = i.doubleValue" or something?


No, you are not required to assign the returned value to anything. I think you might if it's returning a primitive (int, boolean, short, etc), but don't quote me on that.

That code won't even produce any warnings since i is intitialized to null, there's nothing that the compiler would see wrong.

Re: Bringing LOGing Back

2007-04-04 10:06 • by Mcoder
Hey, I was once working at a software that throwed a weard exception indicating a error at Apache's Log4j... After a full day inpecting the error I found the following code:

Logger l = null;
try{
l = new Logger("FileName");
}catch(Exception e){
l.log(e);
}


Of course that was on a library, with no source code available. So I had to reverse engeneer it...

And didn't have permission to fix either. So I fixed the environment, now the software can open the file, but the bug is still there... Quietly waiting for the next unluck developer...

Re: Bringing LOGing Back

2007-04-04 10:13 • by FredDC (unregistered)
130202 in reply to 130186
Gimme your e-mail adress and I'll make sure you get lots!

Re: Bringing LOGing Back

2007-04-04 10:17 • by Asd (unregistered)
130203 in reply to 130201
Mcoder:
Hey, I was once working at a software that throwed a weard exception indicating a error at Apache's Log4j... After a full day inpecting the error I found the following code:

Logger l = null;
try{
l = new Logger("FileName");
}catch(Exception e){
l.log(e);
}


Absolutely brillant. You should have submitted that.

And no you do not need to use the return value of a method call. I can not think of a single language that requires that. Here is a tip: if you have to write "I'm not sure" when discussing basic language features then you probably should not reply at all.

Re: Bringing LOGing Back

2007-04-04 10:20 • by snoofle
130204 in reply to 130199
Ahem, I would like to offer a more enterprisey version:


import java.io.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

try {
String xml = "<doc ...> ... </doc>";
String xpathExp = null;
byte bytes[] = xml.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(bais);
XPath xpath = XPathFactory.newInstance().newXPath();

String data = (String) xpath.evaluate(
xpathExp,
doc,
XPathConstants.STRING);
// Catch all of:
// XPathExpressionException
// ParserConfigurationException
// SAXException
// IOException
// Exception
} catch (Throwable wtf) {
wtf.printStackTrace(PrintStream out);
}

Re: Bringing LOGing Back

2007-04-04 10:23 • by Ed (unregistered)
130205 in reply to 130180
Bob:
Sgt. Preston:
What's degrading about sending or receiving code in attachments?


What's malicious, contains code and usually comes in attachments?


There is a huge difference between opening a TXT file and opening an EXE file. And, since one does not know what sort of funky things the recipient's mail client will do to the code if sent in the body of the email, such as reformatting or attempting to compile, it should be standard practice to send code as an attached TXT file.

Re: Bringing LOGing Back

2007-04-04 10:28 • by DSTMan (unregistered)
130208 in reply to 130203
Asd:
Mcoder:
Hey, I was once working at a software that throwed a weard exception indicating a error at Apache's Log4j... After a full day inpecting the error I found the following code:

Logger l = null;
try{
l = new Logger("FileName");
}catch(Exception e){
l.log(e);
}


Absolutely brillant. You should have submitted that.

And no you do not need to use the return value of a method call. I can not think of a single language that requires that. Here is a tip: if you have to write "I'm not sure" when discussing basic language features then you probably should not reply at all.


Wow. WOW.

That is so much better than even today's hoo-hah.

And regarding the attachment fracas, how about you whiners send your e-mails as PLAIN TEXT?

captcha=stinky ; no comment needed

Re: Bringing LOGing Back

2007-04-04 10:29 • by Gyske
130209 in reply to 130199
Steve:
...
Yes, coding something like
new Throwable().printStackTrace();
is certainly nicely elegant, it isn't precisely intuitively obvious, is it?

I agree 100%. Java just is not the most intuitive, compact or pragmatic language in the world.
And after 2 years, I still regularly use Google(codesearch) when programming in Java.

BTW searchstrings like: "How to <insert task here> in java" often work surprisingly well.

Re: Bringing LOGing Back

2007-04-04 10:32 • by Bavo (unregistered)
Thread.getStackTrace() could be helpful here

Re: Bringing LOGing Back

2007-04-04 10:35 • by Scott Robinson (unregistered)
It seems a lot more likely the attachment included surrounding code that was even more embarrassing.

Re: Bringing LOGing Back

2007-04-04 10:45 • by Ohnonymous (unregistered)
130212 in reply to 130195
Zylon:
So I hereby nominate "As an aside, please stop sending code in attachments. It's degrading to both of us." as the designated REAL WTF of this article.

Second.

Re: Bringing LOGing Back

2007-04-04 10:45 • by Lodo (unregistered)
@ 2007-04-04 09:08 • by Benanov

The .NET 'as' operator in C# works the same way. It'll try the cast and if it fails, you'll see the slowdown and get null.


Try the Environment.StackTrace variable..

Re: Bringing LOGing Back

2007-04-04 10:48 • by mathew (unregistered)
130214 in reply to 130199
Steve:
That's an awful lot of material to assimilate, especially while also trying to keep all of the object oriented concepts and semantics of the language itself straight at the same time.

Searching the web may not necessarily be helpful if you don't know the right search terms to use.


Let me make your life a metric buttload better: http://www.gotapi.com/

I went there imagining I knew nothing about how to produce a stack trace in Java. I clicked J2SE 1.5.0 to pull up the Java documentation, and typed "stack". The result was about a dozen matches. I looked through and found three called "printStackTrace". Two were under java.xml, so obviously not what I wanted. I clicked on the third, and got documentation and examples for how to print a stack trace to standard output, to a PrintWriter, and to a PrintStream.

O'Reilly also used to sell a Java Enterprise CD Bookshelf which had searchable versions of their core Java books. I don't think it has been updated in a while, though; they want you to subscribe to their Safari service instead.

Re: Bringing LOGing Back

2007-04-04 10:51 • by cognac (unregistered)
130215 in reply to 130205
Ed:
Bob:
Sgt. Preston:
What's degrading about sending or receiving code in attachments?


What's malicious, contains code and usually comes in attachments?


There is a huge difference between opening a TXT file and opening an EXE file. And, since one does not know what sort of funky things the recipient's mail client will do to the code if sent in the body of the email, such as reformatting or attempting to compile, it should be standard practice to send code as an attached TXT file.


I'm quite sure there has to be at least one AV prog that will catch source code as well as binaries. After all, source code may as well be some scripted language just waiting for the stupid, eerrr, unsuspecting user to run it.

And since most AV progs hold the users that should not be's hands, well...

Re: Bringing LOGing Back

2007-04-04 10:54 • by ptomblin
Years (ok, decades) ago, I was working on a system that had been ported from Fortran to C. I had just come from a FORTRAN-G environment, so I hated Fortran with a passion, and I made it my side-task to hunt down and kill every piece of Fortran left in the system. The hardest one of them to replace was called whenever the programmer wanted to print out a VMS stack-trace, which he did by calling this Fortran subroutine that divided by zero, because math in Fortran was exception checked but wasn't in C.

I spend a day hunting through the Orange Wall of VMS system documentation, and finally found a function I could call in C that would emit a VMS stack-trace, and declared the war on Fortran over.

Re: Bringing LOGing Back

2007-04-04 10:57 • by NOR (unregistered)
130217 in reply to 130176
RON:
Benanov:
I think .NET does something like this when you ask for a stack trace--it'll throw an exception, catch it, and then give you the stack trace.

The tell-tale "first exception thrown" slowdown is what gave it away to us.

The .NET 'as' operator in C# works the same way. It'll try the cast and if it fails, you'll see the slowdown and get null.


Wrong. Please stop talking if you have no clue what you're talking about.

Please stop talking if you have no intention to enlight the unenlightened.

Re: Bringing LOGing Back

2007-04-04 11:01 • by cconroy
130218 in reply to 130209
Gyske:
Steve:
...
Yes, coding something like
new Throwable().printStackTrace();
is certainly nicely elegant, it isn't precisely intuitively obvious, is it?

I agree 100%. Java just is not the most intuitive, compact or pragmatic language in the world.
And after 2 years, I still regularly use Google(codesearch) when programming in Java.

BTW searchstrings like: "How to <insert task here> in java" often work surprisingly well.



Unless, of course, your task is "hook up a hose to a kitchen sink".

Re: Bringing LOGing Back

2007-04-04 11:06 • by phelyan
130219 in reply to 130199
Steve:
Yes, coding something like
new Throwable().printStackTrace();
is certainly nicely elegant, it isn't precisely intuitively obvious, is it?


And it's not giving you all the additional information of the original snippet...

Re: Bringing LOGing Back

2007-04-04 11:14 • by Rootbeer
130220 in reply to 130205
since one does not know what sort of funky things the recipient's mail client will do to the code if sent in the body of the email, such as reformatting or attempting to compile, it should be standard practice to send code as an attached TXT file.

It would be a WTF indeed for a mail client to do anything to a plaintext message body except display it as plain text. Seriously: "attempting to compile"???

If you were to make an argument that attachments are better because they help distinguish between the code sample and the submitter's own commentary thereupon, though, I'd agree with you...

Re: Bringing LOGing Back

2007-04-04 11:14 • by Gyske
130221 in reply to 130214
mathew:
Steve:
That's an awful lot of material to assimilate, especially while also trying to keep all of the object oriented concepts and semantics of the language itself straight at the same time.

Searching the web may not necessarily be helpful if you don't know the right search terms to use.


Let me make your life a metric buttload better: http://www.gotapi.com/

I've just fallen in love...

Re: Bringing LOGing Back

2007-04-04 11:17 • by Derrick Pallas
130222 in reply to 130193
Sgt. Preston:
That might make the practice inconvenient, but how is it "degrading"?


For me: it degrades my triage and processing speeds.

For you: your submission is being degraded to an F.

Re: Bringing LOGing Back

2007-04-04 11:20 • by zip
130223 in reply to 130182
diaphanein:
Bob:
Sgt. Preston:
What's degrading about sending or receiving code in attachments?


What's malicious, contains code and usually comes in attachments?


Oooh! Oooooooh!! I know this one.... Is it pr0n?


hahahahaa i lol'ed

Re: Bringing LOGing Back

2007-04-04 11:21 • by Anonymous (unregistered)
130224 in reply to 130203
Asd:
And no you do not need to use the return value of a method call. I can not think of a single language that requires that.


Wirthian Pascal does that. Surprisingly, Borland compilers abolished this 'feature' rather quickly.

Re: Bringing LOGing Back

2007-04-04 11:31 • by cparker
130226 in reply to 130221
Gyske:
mathew:
Let me make your life a metric buttload better: http://www.gotapi.com/

I've just fallen in love...
You and me both. I've bookmarked gotapi on del.icio.us and I'm making http://start.gotapi.com/ my startpage. Thank you, mathew. I've been looking for a site like gotapi for a long time.

Re: Bringing LOGing Back

2007-04-04 11:39 • by RON (unregistered)
130227 in reply to 130217
NOR:
RON:
Benanov:
I think .NET does something like this when you ask for a stack trace--it'll throw an exception, catch it, and then give you the stack trace.

The tell-tale "first exception thrown" slowdown is what gave it away to us.

The .NET 'as' operator in C# works the same way. It'll try the cast and if it fails, you'll see the slowdown and get null.


Wrong. Please stop talking if you have no clue what you're talking about.

Please stop talking if you have no intention to enlight the unenlightened.


I did. I enlightened him by telling him that he is completely, utterly, 100% wrong. He postulated that getting a stack trace and using the 'as' operator threw exceptions under the hood when used. They don't. That's all there is to it.

I have very little tolerance or patience for people who spread bad and just plain wrong information using an authoritative tone. He said "The .NET 'as' operator in C# works the same way. It'll try the cast and if it fails, you'll see the slowdown and get null.", as if he knew exactly what happens, when in fact nothing of the sort ever happens. If he's such an authority on things, he should be able to take the effort to re-educate himself on why he's completely and utterly 100% wrong.


QED.
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment