Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Additional logging questions

Reply
Thread Tools

Additional logging questions

 
 
Novice
Guest
Posts: n/a
 
      02-27-2012

I _think_ these supplementary questions will be a lot less of a struggle
than the others in "Aspect questions" thread. That thread is getting on
the long side so I thought I'd ask these in a new thread.

Basically, I'm looking for advice on what should always be logged by
every class. I understand now that every class is going to have its own
logger but what should be logged?

Or to put it another way, are there cases where a class won't log at all?

I'm thinking of things like Enums. If I have an enum that lists the days
of the week, there's not much to go wrong there and I'm not likely to
throw exceptions or even have a try/catch block. So should it just be
left so that it isn't logging at all? Or should there be some standard
bare-minimum sort of logging, like an entering() and existing(), even if
nothing else of interest goes on?

What about holder classes? I'm not sure if I'm using the terminology
correctly but I'm thinking of a class where you simply store related bits
of data, like a Name class whose constructor insists on a first name and
a last name and then supplies getters and setters so that another class
can ask for just the first name or just the last name? (Let's pretend
that everyone has exactly one given name and one surname, no exceptions,
just to keep this simple). This could be an awfully barebones class if it
only had a two line constructor and one line getters and setters. Should
it log anyway?

My feeling is that Lew would say NOT to log unless there was a good
reason to log and then cite several good reasons to log. I'm not sure if
something like an enum or a holder class (if I've used the term
correctly) would EVER justify logging though.

I may have completely misread Lew and, if so, I'm sorry. Maybe this is
another premature leap....

Some of the rest of you may differ dramatically on what should be logged
and when it is okay not to bother. I hope some of you can share those
thoughts with me.

Basically, I'm just about ready to start getting loggers for each and
every class in the project I'm working on now (with plans to do the same
in every project as I create it or return to it). But I don't want to do
too much logging either.....

--
Novice
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-27-2012
On 2/26/2012 9:02 PM, Novice wrote:
> I _think_ these supplementary questions will be a lot less of a struggle
> than the others in "Aspect questions" thread. That thread is getting on
> the long side so I thought I'd ask these in a new thread.
>
> Basically, I'm looking for advice on what should always be logged by
> every class. I understand now that every class is going to have its own
> logger but what should be logged?
>
> Or to put it another way, are there cases where a class won't log at all?
>
> I'm thinking of things like Enums. If I have an enum that lists the days
> of the week, there's not much to go wrong there and I'm not likely to
> throw exceptions or even have a try/catch block. So should it just be
> left so that it isn't logging at all? Or should there be some standard
> bare-minimum sort of logging, like an entering() and existing(), even if
> nothing else of interest goes on?
>
> What about holder classes? I'm not sure if I'm using the terminology
> correctly but I'm thinking of a class where you simply store related bits
> of data, like a Name class whose constructor insists on a first name and
> a last name and then supplies getters and setters so that another class
> can ask for just the first name or just the last name? (Let's pretend
> that everyone has exactly one given name and one surname, no exceptions,
> just to keep this simple). This could be an awfully barebones class if it
> only had a two line constructor and one line getters and setters. Should
> it log anyway?
>
> My feeling is that Lew would say NOT to log unless there was a good
> reason to log and then cite several good reasons to log. I'm not sure if
> something like an enum or a holder class (if I've used the term
> correctly) would EVER justify logging though.
>
> I may have completely misread Lew and, if so, I'm sorry. Maybe this is
> another premature leap....
>
> Some of the rest of you may differ dramatically on what should be logged
> and when it is okay not to bother. I hope some of you can share those
> thoughts with me.
>
> Basically, I'm just about ready to start getting loggers for each and
> every class in the project I'm working on now (with plans to do the same
> in every project as I create it or return to it). But I don't want to do
> too much logging either.....


You should log the information you expect potentially could be
useful when troubleshooting a problem.

And as a general rule, then if any doubt then log, because it
is usually better to have too much logging than too little
logging.

I do not see any need for logging in an enum or in a pure
data class (holder class).

But please add a toString method in your data class, so
when the class with real login in that uses the data class
can log it and you get something useful in the log about the
data.

Arne


 
Reply With Quote
 
 
 
 
Novice
Guest
Posts: n/a
 
      02-27-2012
Arne Vajh°j <(E-Mail Removed)> wrote in
news:4f4ae583$0$281$(E-Mail Removed):

> On 2/26/2012 9:02 PM, Novice wrote:
>> I _think_ these supplementary questions will be a lot less of a
>> struggle than the others in "Aspect questions" thread. That thread is
>> getting on the long side so I thought I'd ask these in a new thread.
>>
>> Basically, I'm looking for advice on what should always be logged by
>> every class. I understand now that every class is going to have its
>> own logger but what should be logged?
>>
>> Or to put it another way, are there cases where a class won't log at
>> all?
>>
>> I'm thinking of things like Enums. If I have an enum that lists the
>> days of the week, there's not much to go wrong there and I'm not
>> likely to throw exceptions or even have a try/catch block. So should
>> it just be left so that it isn't logging at all? Or should there be
>> some standard bare-minimum sort of logging, like an entering() and
>> existing(), even if nothing else of interest goes on?
>>
>> What about holder classes? I'm not sure if I'm using the terminology
>> correctly but I'm thinking of a class where you simply store related
>> bits of data, like a Name class whose constructor insists on a first
>> name and a last name and then supplies getters and setters so that
>> another class can ask for just the first name or just the last name?
>> (Let's pretend that everyone has exactly one given name and one
>> surname, no exceptions, just to keep this simple). This could be an
>> awfully barebones class if it only had a two line constructor and one
>> line getters and setters. Should it log anyway?
>>
>> My feeling is that Lew would say NOT to log unless there was a good
>> reason to log and then cite several good reasons to log. I'm not sure
>> if something like an enum or a holder class (if I've used the term
>> correctly) would EVER justify logging though.
>>
>> I may have completely misread Lew and, if so, I'm sorry. Maybe this
>> is another premature leap....
>>
>> Some of the rest of you may differ dramatically on what should be
>> logged and when it is okay not to bother. I hope some of you can
>> share those thoughts with me.
>>
>> Basically, I'm just about ready to start getting loggers for each and
>> every class in the project I'm working on now (with plans to do the
>> same in every project as I create it or return to it). But I don't
>> want to do too much logging either.....

>
> You should log the information you expect potentially could be
> useful when troubleshooting a problem.
>
> And as a general rule, then if any doubt then log, because it
> is usually better to have too much logging than too little
> logging.
>
> I do not see any need for logging in an enum or in a pure
> data class (holder class).
>
> But please add a toString method in your data class, so
> when the class with real login in that uses the data class
> can log it and you get something useful in the log about the
> data.
>

Sorry, I'm not following you.

Are you saying that the toString() method needs to be there to turn
things like references into meaningful information? I know that a
reference to something like a JFrame is not going to be very meaningful
and would rather display the name given the JFrame via setName(). Or are
you saying something quite different?


--
Novice
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-27-2012
Novice wrote:
> Arne Vajh├Şj wrote:
>> Novice wrote:
>>> Basically, I'm looking for advice on what should always be logged by
>>> every class. I understand now that every class is going to have its
>>> own logger but what should be logged?


You log what reveals to someone reading the log what they want to know.

>>> Or to put it another way, are there cases where a class won't log at
>>> all?


That depends on the logging level set at runtime, doesn't it?

If a class is unable to log anything, and that omission deprives someone of
necessary information when they're relying on the log, it's a mistake.

If no one ever needs information from that class out of the log, it can get
away with not logging.

>>> I'm thinking of things like Enums. If I have an enum that lists the


Enums are classes. The same considerations apply as for any other class.

>>> days of the week, there's not much to go wrong there and I'm not


Is there? I trust you - there's not.

>>> likely to throw exceptions or even have a try/catch block. So should
>>> it just be left so that it isn't logging at all? Or should there be
>>> some standard bare-minimum sort of logging, like an entering() and
>>> existing(), even if nothing else of interest goes on?


Good questions. Answer wisely, Grasshopper.

>>> What about holder classes? I'm not sure if I'm using the terminology


You are.

>>> correctly but I'm thinking of a class where you simply store related
>>> bits of data, like a Name class whose constructor insists on a first
>>> name and a last name and then supplies getters and setters so that
>>> another class can ask for just the first name or just the last name?
>>> (Let's pretend that everyone has exactly one given name and one
>>> surname, no exceptions, just to keep this simple). This could be an
>>> awfully barebones class if it only had a two line constructor and one
>>> line getters and setters. Should it log anyway?


Logging is generally for state changes.

>>> My feeling is that Lew would say NOT to log unless there was a good
>>> reason to log and then cite several good reasons to log. I'm not sure


Don't take advice from your fantasy of me unless it's good advice. Don't take
advice from the real me unless it's good advice, either.

>>> if something like an enum or a holder class (if I've used the term
>>> correctly) would EVER justify logging though.


Sure.

Depends on what's in it, doesn't it?

>>> I may have completely misread Lew and, if so, I'm sorry. Maybe this
>>> is another premature leap....


Since I never before said what to log or not log that you've seen, there's
been nothing to misread, has there?

Nothing to read, nothing to misread. It's a simple equation.

>>> Some of the rest of you may differ dramatically on what should be
>>> logged and when it is okay not to bother. I hope some of you can
>>> share those thoughts with me.
>>>
>>> Basically, I'm just about ready to start getting loggers for each and
>>> every class in the project I'm working on now (with plans to do the
>>> same in every project as I create it or return to it). But I don't
>>> want to do too much logging either.....

>>
>> You should log the information you expect potentially could be
>> useful when troubleshooting a problem.


This requires that you think like a useful person, not a computer programmer.

When you're troubleshooting a log, you don't have code in front of you. You
have what the log tells you. It had better God-damned tell you what you need,
because you wouldn't be looking if someone weren't breathing down your neck.
No fancy "***********************=============" strings. Logs are dense,
multi-mega- or gigabyte beasts of tightly printed strings.

Ops personnel read logs. Ops personnel think programmers are children. I had
an ops mentor who told me, "We love getting the programmers from [the
development location] here for six months. They go back to coding _changed_!"

Other times they're cursing the programmers who wrote such lame logging
statements.

>> And as a general rule, then if any doubt then log, because it
>> is usually better to have too much logging than too little
>> logging.
>>
>> I do not see any need for logging in an enum or in a pure
>> data class (holder class).


Some enums.

Like any other class, it depends on what it does. But generally you log state
changes, i.e., behavioral methods (not usually attributes). You log anything
that is weird. You log errors and warnings.

You pick appropriate logging levels. Here's my log4j idiom:

public void loadResource()
{
logger.debug("");

final BufferedReader reader;
try
{
reader = new BufferedReader(new InputStreamReader(getClass()
.getResourceAsStream("/config/configuration.txt")));
}
catch(IOException exc)
{
String msg = "Cannot open configuration. "+ exc.getLocalizedMessage();
logger.error(msg, exc);
throw new IllegalStateException(msg, exc);
}
assert reader != null;

try
{
// read the Reader, etc.
}
catch(IOException exc)
{
String msg = "Cannot read configuration. "+ exc.getLocalizedMessage();
logger.error(msg, exc);
throw new IllegalStateException(msg, exc);
}
finally
{
try
{
reader.close();
}
catch(IOException exc)
{
String msg = "Cannot close configuration. "
+ exc.getLocalizedMessage();
logger.warn(msg, exc);
}
}
}

Note the multiple uses of 'logger' (an instance member) in that method.

>> But please add a toString method in your data class, so
>> when the class with real login in that uses the data class
>> can log it and you get something useful in the log about the
>> data.
>>

> Sorry, I'm not following you.
>
> Are you saying that the toString() method needs to be there to turn
> things like references into meaningful information? I know that a
> reference to something like a JFrame is not going to be very meaningful
> and would rather display the name given the JFrame via setName(). Or are
> you saying something quite different?


'toString()' should always give a useful way to identify the specific instance.

It should depend on (and usually only on) the same fields used to drive
'hashCode()' and 'equals()' and if supported, 'compareTo()' (which should
always be consistent with each other).

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Novice
Guest
Posts: n/a
 
      02-27-2012
Lew <(E-Mail Removed)> wrote in news:jif6ua$3cm$(E-Mail Removed):

> Novice wrote:
>> Arne Vajh├Şj wrote:
>>> Novice wrote:
>>>> Basically, I'm looking for advice on what should always be logged
>>>> by every class. I understand now that every class is going to have
>>>> its own logger but what should be logged?

>
> You log what reveals to someone reading the log what they want to
> know.
>
>>>> Or to put it another way, are there cases where a class won't log
>>>> at all?

>
> That depends on the logging level set at runtime, doesn't it?
>

Right.

> If a class is unable to log anything, and that omission deprives
> someone of necessary information when they're relying on the log, it's
> a mistake.
>

Another good rule of thumb.

> If no one ever needs information from that class out of the log, it
> can get away with not logging.
>

That seems reasonable to me. I was pretty sure you were not going to
advocate logging for the sake of logging and you didn't let me down

>>>> I'm thinking of things like Enums. If I have an enum that lists the

>
> Enums are classes. The same considerations apply as for any other
> class.
>
>>>> days of the week, there's not much to go wrong there and I'm not

>
> Is there? I trust you - there's not.
>

Well, I'm thinking of enums like mine which are akin to edits. For
instance, I use preferences for some of my programs so I created a
PreferenceTrees enum. It has only two values, SYSTEM and USER. My
PreferenceUtils class uses PreferenceTrees as a type in all of its
methods so that someone invoking those methods can ONLY choose
PreferenceTrees.SYSTEM or PreferenceTrees.USER, there's no possibility
that someone is going to misspell "System", which could certainly happen
if the method was expecting a String parameter. The PreferencesTree enum
is trivial and seems unlikely to ever blow up.

But if an enum did more complicated logic, like looking up a day number
and turning it into a day name in a foreign language, then sure, I can
see that something might go awry and justify logging. The logic that
looks up how to say "Monday" in Latvian or Turkish could throw an
exception that should be logged. But my enums are mostly like the
PreferenceTrees enum that I mentioned.


>>>> likely to throw exceptions or even have a try/catch block. So
>>>> should it just be left so that it isn't logging at all? Or should
>>>> there be some standard bare-minimum sort of logging, like an
>>>> entering() and existing(), even if nothing else of interest goes
>>>> on?

>
> Good questions. Answer wisely, Grasshopper.
>
>>>> What about holder classes? I'm not sure if I'm using the
>>>> terminology

>
> You are.
>

Some people call these data classes too, if I'm not mistaken. I find that
a bit more descriptive but maybe that's just me.

>>>> correctly but I'm thinking of a class where you simply store
>>>> related bits of data, like a Name class whose constructor insists
>>>> on a first name and a last name and then supplies getters and
>>>> setters so that another class can ask for just the first name or
>>>> just the last name? (Let's pretend that everyone has exactly one
>>>> given name and one surname, no exceptions, just to keep this
>>>> simple). This could be an awfully barebones class if it only had a
>>>> two line constructor and one line getters and setters. Should it
>>>> log anyway?

>
> Logging is generally for state changes.
>
>>>> My feeling is that Lew would say NOT to log unless there was a good
>>>> reason to log and then cite several good reasons to log. I'm not
>>>> sure

>
> Don't take advice from your fantasy of me unless it's good advice.
> Don't take advice from the real me unless it's good advice, either.
>

Absolutely. I'm just trying to think along the same lines as you've
proposed in your posts. I hope I haven't made an unwarranted leap again.

>>>> if something like an enum or a holder class (if I've used the term
>>>> correctly) would EVER justify logging though.

>
> Sure.
>
> Depends on what's in it, doesn't it?
>

See my remarks about enums above. The same would apply to a holder class.
If it's a one line getter or setter, there's probably not much to go
wrong and logging is probably inappropriate. But if it's doing something
that could fail, sure, logging would make sense.

>>>> I may have completely misread Lew and, if so, I'm sorry. Maybe this
>>>> is another premature leap....

>
> Since I never before said what to log or not log that you've seen,
> there's been nothing to misread, has there?
>
> Nothing to read, nothing to misread. It's a simple equation.
>

Understood. I'm just extrapolating the general principle that you've been
stating, which I would paraphrase (roughly) as "don't do anything
slavishly or unnecessarily; do it because it makes sense to do it".

>>>> Some of the rest of you may differ dramatically on what should be
>>>> logged and when it is okay not to bother. I hope some of you can
>>>> share those thoughts with me.
>>>>
>>>> Basically, I'm just about ready to start getting loggers for each
>>>> and every class in the project I'm working on now (with plans to do
>>>> the same in every project as I create it or return to it). But I
>>>> don't want to do too much logging either.....
>>>
>>> You should log the information you expect potentially could be
>>> useful when troubleshooting a problem.

>
> This requires that you think like a useful person, not a computer
> programmer.
>
> When you're troubleshooting a log, you don't have code in front of
> you. You have what the log tells you. It had better God-damned tell
> you what you need, because you wouldn't be looking if someone weren't
> breathing down your neck. No fancy
> "***********************=============" strings. Logs are dense,
> multi-mega- or gigabyte beasts of tightly printed strings.
>
> Ops personnel read logs. Ops personnel think programmers are children.
> I had an ops mentor who told me, "We love getting the programmers from
> [the development location] here for six months. They go back to coding
> _changed_!"
>
> Other times they're cursing the programmers who wrote such lame
> logging statements.
>

I have had very little contact with operators in the PC era but I did
have some in my mainframe days. Back then, they didn't have a lot to do
with fixing the problems in the sense of repairing the code. Their job
was basically to figure out which program had bombed and then look that
up in their list so they knew which programmer to call.

Would I be right in assuming that it's pretty much the same situation
today in a massively PC-oriented world? Or have they assumed many new
responsibilities?

It would help a lot to know what they hope to find in a log.

If they still operate like they did in my mainframe days, I expect they
pretty much just want to know which program failed so they can look up
the on-call programmer's name. They won't care about most details,
although they might like to be able to tell the programmer "Program Foo
crashed on an IllegalArgumentException in the constructor for class
FooMainPanel" as opposed to just "Program Foo crashed". They won't care
about stacktraces or such things. But the programmer is going to care a
lot about the stacktraces and other information!

But maybe modern operators do a lot more than that. You seem very
familiar with what they do so this would be a great chance to get your
insight on this.


>>> And as a general rule, then if any doubt then log, because it
>>> is usually better to have too much logging than too little
>>> logging.
>>>
>>> I do not see any need for logging in an enum or in a pure
>>> data class (holder class).

>
> Some enums.
>
> Like any other class, it depends on what it does. But generally you
> log state changes, i.e., behavioral methods (not usually attributes).
> You log anything that is weird. You log errors and warnings.
>
> You pick appropriate logging levels. Here's my log4j idiom:
>
> public void loadResource()
> {
> logger.debug("");
>
> final BufferedReader reader;
> try
> {
> reader = new BufferedReader(new InputStreamReader(getClass()
> .getResourceAsStream("/config/configuration.txt")));
> }
> catch(IOException exc)
> {
> String msg = "Cannot open configuration. "+
> exc.getLocalizedMessage(); logger.error(msg, exc);
> throw new IllegalStateException(msg, exc);
> }
> assert reader != null;
>
> try
> {
> // read the Reader, etc.
> }
> catch(IOException exc)
> {
> String msg = "Cannot read configuration. "+
> exc.getLocalizedMessage(); logger.error(msg, exc);
> throw new IllegalStateException(msg, exc);
> }
> finally
> {
> try
> {
> reader.close();
> }
> catch(IOException exc)
> {
> String msg = "Cannot close configuration. "
> + exc.getLocalizedMessage();
> logger.warn(msg, exc);
> }
> }
> }
>
> Note the multiple uses of 'logger' (an instance member) in that
> method.
>

Interesting. I'm going to need to imitate that...

>>> But please add a toString method in your data class, so
>>> when the class with real login in that uses the data class
>>> can log it and you get something useful in the log about the
>>> data.
>>>

>> Sorry, I'm not following you.
>>
>> Are you saying that the toString() method needs to be there to turn
>> things like references into meaningful information? I know that a
>> reference to something like a JFrame is not going to be very
>> meaningful and would rather display the name given the JFrame via
>> setName(). Or are you saying something quite different?

>
> 'toString()' should always give a useful way to identify the specific
> instance.
>

That's why I was displaying the name of the table so I'm going to take
that as a "yes"

> It should depend on (and usually only on) the same fields used to
> drive 'hashCode()' and 'equals()' and if supported, 'compareTo()'
> (which should always be consistent with each other).
>

I don't touch hashCode(), equals() or compareTo() very often at all. Or
toString() either for that matter. But overriding hashCode() and equals()
solved a big problem for me recently. I was writing a holder class that
contained three fields, a table name, a row number and a column number,
as a key for a hash map and then storing the column width in the value
portion of the map. But when I tried to look up a given combination of
table name, row number and column number I was never finding values that
I knew were there. I googled and found out that I needed to revise
hashCode() and equals() and rougly what those changes needed to be and
then my lookups went fine. But I didn't touch compareTo(). Hmm, maybe I
need to revisit that and make sure it doesn't need some tweaking too....

--
Novice
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-27-2012
On 02/27/2012 11:14 AM, Novice wrote:
> I don't touch hashCode(), equals() or compareTo() very often at all. Or
> toString() either for that matter. But overriding hashCode() and equals()
> solved a big problem for me recently. I was writing a holder class that
> contained three fields, a table name, a row number and a column number,
> as a key for a hash map and then storing the column width in the value
> portion of the map. But when I tried to look up a given combination of
> table name, row number and column number I was never finding values that
> I knew were there. I googled and found out that I needed to revise
> hashCode() and equals() and rougly what those changes needed to be and
> then my lookups went fine. But I didn't touch compareTo(). Hmm, maybe I
> need to revisit that and make sure it doesn't need some tweaking too....


Read the Javadocs for those methods.

'compareTo()' is not universal, being present only in instances of
'Comparable<T>'.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-27-2012
On 2/26/12 6:02 PM, Novice wrote:
> I _think_ these supplementary questions will be a lot less of a struggle
> than the others in "Aspect questions" thread. That thread is getting on
> the long side so I thought I'd ask these in a new thread.
>
> Basically, I'm looking for advice on what should always be logged by
> every class. I understand now that every class is going to have its own
> logger but what should be logged?
>
> Or to put it another way, are there cases where a class won't log at all?


Classes don't log, methods log. The question then becomes, which methods
should (or shouldn't) log.

What I've determined is that you need to ask yourself a series of questions.

Would logging at this point be useful at all?
Who would it be useful for? Developers, Operations, and/or business?
How much information should I include? Just a message stating an event
happened? Metrics about that event? Details about all objects involved?
What log level should I use?

For example, if you think a Developer needs this information, but no one
else does, then a "debug" level log with all relevant details makes
sense. Operations and/or business probably just want metrics.
Operations just want errors or severe warnings.

Also, depending on where the code runs, you may enable/disable certain
levels of logging. Production probably only warnings/errors should be
enabled after start up, and info should be enabled only during start-up.
In UAT/QA, more information seems useful. Enabling debug makes sense in
DEV environments, *or* when you have a problem you can't reproduce in a
dev environment.

It is also possible (though rare) to have a class have multiple loggers.
I can think of exactly one use-case, but there may be more. That
use-case is specifically for metric logging. If you have some metrics
your code collects to log, then it might make sense to separate those
out into there own "log stream".

Hope this helps,
Daniel.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-27-2012
Novice wrote:
> Lew wrote:
>> 'toString()' should always give a useful way to identify the specific
>> instance.
>>

> That's why I was displaying the name of the table so I'm going to take
> that as a "yes"
>
>> It should depend on (and usually only on) the same fields used to
>> drive 'hashCode()' and 'equals()' and if supported, 'compareTo()'
>> (which should always be consistent with each other).
>>

> I don't touch hashCode(), equals() or compareTo() very often at all. Or
> toString() either for that matter. But overriding hashCode() and equals()
> solved a big problem for me recently. I was writing a holder class that
> contained three fields, a table name, a row number and a column number,
> as a key for a hash map and then storing the column width in the value
> portion of the map. But when I tried to look up a given combination of
> table name, row number and column number I was never finding values that
> I knew were there. I googled and found out that I needed to revise
> hashCode() and equals() and rougly what those changes needed to be and
> then my lookups went fine. But I didn't touch compareTo(). Hmm, maybe I
> need to revisit that and make sure it doesn't need some tweaking too....


The default 'Object#equals()' method gives (almost) the same answer as the ==
operator - two instances are equal iff (if and only if) they are the same
instance. That's called "object equality" or "instance equality".

Often we want value equality. It's not very helpful, for example, for

new Integer(67551) != new Integer(67551)

to be true.

That means equality of different instances, provided their values match in
some defined way.

For 'Integer', value equality depends on only one attribute, the underlying
'int' field.

For a more complicated type, there might be more than one attribute. For
example, you might regard two widgets as equal iff they have the same owner
and color:

public class Widget
{
private final Owner owner; // assume getter methods for brevity
private final Color color;
public Widget(Owner owner, Color color)
{
if (owner == null || color == null)
{
throw new IllegalArgumentException("null");
}
this.owner = owner;
this.color = color'
assert this.owner != null && this.color != null;
}
@Override public boolean equals(Object other)
{
if (other == this) { return true; }
if (! (other instanceof Widget)) { return false; }
Widget widget = (Widget) other;
return this.getOwner().equals(widget.getOwner())
&& this.getColor().equals(widget.getColor()
}
}

But wait! 'hashCode()', which is a shortcut for 'equals()' (well, really for
"not equals()"), doesn't know anything about those fields yet! It will think
objects are not equal when they really are.

So you have to override 'hashCode()', too, using the same fields, so that its
result are consistent with 'equals()':

@Override public int hashCode()
{
return owner.hashCode() * 31 + color.hashCode();
}

Since 'compareTo()' also makes claims about equality, it must agree with the
other two methods when it exists.

Since the fields that determine equality identify the value by definition,
they should determine the 'toString()' result also:

@Override public String toString()
{
assert owner != null && color != null;
return '{'+ owner +", "+ color + '}';
}

As you can see, 'toString()' can include additional information, but it must
show what identifies the value.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-28-2012
On 2/26/2012 11:12 PM, Novice wrote:
> Arne Vajh°j<(E-Mail Removed)> wrote in
>> But please add a toString method in your data class, so
>> when the class with real login in that uses the data class
>> can log it and you get something useful in the log about the
>> data.
>>

> Sorry, I'm not following you.
>
> Are you saying that the toString() method needs to be there to turn
> things like references into meaningful information? I know that a
> reference to something like a JFrame is not going to be very meaningful
> and would rather display the name given the JFrame via setName(). Or are
> you saying something quite different?


I am saying that.

Object toString is not very useful so you want to
override with something that returns relevant information.

Arne

 
Reply With Quote
 
Novice
Guest
Posts: n/a
 
      02-28-2012
Daniel Pitts <(E-Mail Removed)> wrote in
news:GPR2r.17474$(E-Mail Removed):

> On 2/26/12 6:02 PM, Novice wrote:
>> I _think_ these supplementary questions will be a lot less of a
>> struggle than the others in "Aspect questions" thread. That thread is
>> getting on the long side so I thought I'd ask these in a new thread.
>>
>> Basically, I'm looking for advice on what should always be logged by
>> every class. I understand now that every class is going to have its
>> own logger but what should be logged?
>>
>> Or to put it another way, are there cases where a class won't log at
>> all?

>
> Classes don't log, methods log.


You're right of course. I was just shorthanding by talking about classes
logging.

>The question then becomes, which
> methods should (or shouldn't) log.
>
> What I've determined is that you need to ask yourself a series of
> questions.
>
> Would logging at this point be useful at all?
> Who would it be useful for? Developers, Operations, and/or business?
> How much information should I include? Just a message stating an event
> happened? Metrics about that event? Details about all objects
> involved? What log level should I use?
>
> For example, if you think a Developer needs this information, but no
> one else does, then a "debug" level log with all relevant details
> makes sense. Operations and/or business probably just want metrics.
> Operations just want errors or severe warnings.
>
> Also, depending on where the code runs, you may enable/disable certain
> levels of logging. Production probably only warnings/errors should be
> enabled after start up, and info should be enabled only during
> start-up. In UAT/QA, more information seems useful. Enabling debug
> makes sense in DEV environments, *or* when you have a problem you
> can't reproduce in a dev environment.
>
> It is also possible (though rare) to have a class have multiple
> loggers.
> I can think of exactly one use-case, but there may be more. That
> use-case is specifically for metric logging. If you have some metrics
> your code collects to log, then it might make sense to separate those
> out into there own "log stream".
>
> Hope this helps,


It does! Thanks Daniel!

--
Novice
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TkHTML progress, additional questions Randy Kramer Ruby 0 04-03-2006 04:03 PM
logging buffered vs. logging history Christian Roos Cisco 4 02-05-2006 10:55 PM
Additional Repeater questions for Steven Cheng Mark ASP .Net 2 01-28-2004 11:25 AM
[java.util.logging] logging only to _one_ file Stefan Siegl Java 0 08-27-2003 12:29 PM
Additional questions in 70-300 exam Kael V. Dowdy MCSD 1 08-05-2003 01:40 PM



Advertisments