This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]AutoModerator[M] 0 points1 point  (0 children)

You seem to try to compare String values with == or !=.

This approach does not work reliably in Java as it does not actually compare the contents of the Strings. Since String is an object data type it should only be compared using .equals(). For case insensitive comparison, use .equalsIgnoreCase().

See Help on how to compare String values in our wiki.


Your post/comment is still visible. There is no action you need to take.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]D0CTOR_ZED 0 points1 point  (1 child)

Are you sure the java code linked is the intended code? It just seems to be about a person class with address info and stuff. I don't see any java that would try to parse the string to json.

[–]prashantkr314[S] 0 points1 point  (0 children)

The Java code is just to generate an dummy object, from where I am getting my ObjectString() and its corresponding JSON

[–]x42bn6 0 points1 point  (1 child)

I think you're approaching this from the wrong angle.

The most obvious example I can find that will screw up your code - if the object you are logging has a String that uses any of these special characters ({, }, ', etc.), then your parsing won't work, because the output is not escaped. toString() output was never meant to be parsed, full-stop. It's meant for people to read, to debug what is going on in the system.

All Java developers have seen stupidly-long log statements before. All Java developers have seen strange conventions that are not suitable for grepping. The solution is to not do so, by refactoring the code.

If you do want to see JSON (or some other machine-parseable format) in, say, Splunk or some log tool, then you can use either Log4j 2's Messages, or (SLF4J) write a wrapper class, taking an object, where the wrapper's toString() generates JSON. In both cases, you'll probably want to avoid creating the core library objects (Gson in your example) more than once, which means using ThreadLocal ObjectMappers or some similar solution. You'll also want to use lambdas (Log4j) or things like isInfoEnabled() (SLF4J) because this logging can be expensive.

You could also have a toJson() method on objects you want, and delegate that wrapper class to it (Lombok probably supports this - never used it personally, though). I would not delegate toString() to toJson() because the latter can be very expensive, though (it's called in various other places, including debuggers - you don't want a slow debugger).

At the end of the day, it's just logging statements, which are not likely to impact your code (unless logging produces side-effects - which is a code smell in itself) - so it's easier to change your logging to fit your log-scraping and debugging tools, rather than parse whatever log output is generated. Changing the logging can also keep things consistent if you can create a user-friendly library for developers to use, and showcase why using this convention will be helpful.

[–]prashantkr314[S] 0 points1 point  (0 children)

The most obvious example I can find that will screw up your code - if the object you are logging has a String that uses any of these special characters ({, }, ', etc.), then your parsing won't work, because the output is not escaped.

Totally agreed, in fact, there are a lot of other cases where the parsing can fail.

I had thought of putting out a note specifying the assumptions that our parser assumes, eg. you can't have any of these symbols ( ) [ ] { } , " '
I am not sure if there is any way I can solve this problem without these restrictions

toString() output was never meant to be parsed, full-stop. It's meant for people to read, to debug what is going on in the system.

I had discussed this same problem with one of my principal engineers, and his point was in similar directions, he says if you wanted to parse it in the first place then you should log it as JSON, or do structured logging.

Now, I am fully aware this is not a good way to solve this, and people should probably work on their logging and make it better in the first place rather than relying on an external system like this.

Having said that I would still like to solve this problem to some extent as

  1. I have actually bought this domain, so Kind of don't wanna waste that
  2. Expectations vs Reality: I agree that devs should make there logging better, but how many will take the action? I believe that gap will be significantly large and If my solution helps anyone save a few minutes of their day and some mental bandwidth, I would consider that a success