>
> So I am planning to log in-case of Java object as below without logging
> whole object.
> { "javaObject" : true, "hashCode" : "39d92a", "class" :
> "org.wso2.apim.StoreHostObject"}
>
-1

   - Think from the JavaScript perspective. When you log an object in
   Jaggery/JavaScript it gives you the JSON representation of that object.
   That is the expected behavior. So if you outputs a JSON string as the above
   suggestion, it gives a wrong impression that there are 3 properties
   "javaObject", "hasCode" & "class".
   - Most of the time we log an object to see what is inside that object.
   Above suggestion does not help in that situation. See the beginning of this
   mail thread. The original problem you had was 'Jaggery gives "{}" when
   logging a POJO instead of the JSON representation of that POJO'.

My suggestion is to just output the JSON string of the POJO, If you really
want to add the Java class name then you can try something like this.
 (org.wso2.models.Student){"name": "Kamal", "age": "26", "city": "Colombo"}

Thanks.

On Wed, Feb 3, 2016 at 1:00 PM, Rajeenthini Satkunam <[email protected]>
wrote:

> HI all,
>
> The issue was solved by adding <importBundleDef> for
> jackson-databind,jackson-annotations,jackson-core for POM[1] .
>
> By the Way I would like to discuss more regarding this mail.
>
> *Issue I am trying to solve*
>
>  - I am getting empty JSON string ("{}") when logging Java object.
>  - Empty JSON string("{}") will give wrong information to user.
>
> *Proposed solution*
>
>   - give the user proper understandable log message.
>   - the logging need to be fast and summarized.
>
> So I am planning to log in-case of Java object as below without logging
> whole object.
> { "javaObject" : true, "hashCode" : "39d92a", "class" :
> "org.wso2.apim.StoreHostObject"}
>
> You can find these links[2][3] as resources to get an idea.
>
> [1] -
> https://github.com/wso2/jaggery/blob/master/features/org.jaggeryjs.server.feature/pom.xml
> [2] -
> https://github.com/wso2/jaggery/blob/master/components/hostobjects/org.jaggeryjs.hostobjects.log/src/main/java/org/jaggeryjs/hostobjects/log/LogHostObject.java
> [3] -
> https://github.com/wso2/jaggery/blob/master/components/script-engine/org.jaggeryjs.scriptengine/src/main/java/org/jaggeryjs/scriptengine/util/HostObjectUtil.java
>
>
> On Wed, Feb 3, 2016 at 10:27 AM, Rajeenthini Satkunam <
> [email protected]> wrote:
>
>> HI Niranjan,
>>
>> Ya sure.I will meet you today.
>>
>> On Wed, Feb 3, 2016 at 9:25 AM, Niranjan Karunanandham <[email protected]
>> > wrote:
>>
>>> Hi Rajeenthini,
>>>
>>>
>>> On Wed, Feb 3, 2016 at 9:19 AM, Rajeenthini Satkunam <
>>> [email protected]> wrote:
>>>
>>>> HI All,
>>>>
>>>> Thank you chamara and sajith for your valuable suggestions.I have done
>>>> the improvement in Jaggery to log the java Object.
>>>>
>>>> improvement
>>>> -----------------
>>>> 1)
>>>> https://github.com/wso2/jaggery/blob/master/components/script-engine/org.jaggeryjs.scriptengine/src/main/java/org/jaggeryjs/scriptengine/util/HostObjectUtil.java#L78
>>>>
>>>> Added new code segment as per sajith's suggestion
>>>>
>>>> try{
>>>>     return (new ObjectMapper().writeValueAsString(obj));
>>>> }catch (Exception e){
>>>>     log.debug("Object " + obj.toString() + " of class " + obj.getClass() + 
>>>> " cannot be converted to JSON");
>>>> }
>>>>
>>>> return "{}";
>>>>
>>>>
>>>> 2) https://github.com/wso2/jaggery/blob/master/pom.xml
>>>>
>>>> Added maven dependencies for Jackson data-bind and annotations
>>>>
>>>> <dependency>
>>>>     <groupId>com.fasterxml.jackson.core</groupId>
>>>>     <artifactId>jackson-annotations</artifactId>
>>>>     <version>${jackson.version}</version>
>>>> </dependency>
>>>> <dependency>
>>>>     <groupId>com.fasterxml.jackson.core</groupId>
>>>>     <artifactId>jackson-databind</artifactId>
>>>>     <version>${jackson.version}</version>
>>>> </dependency>
>>>>
>>>> 3) 
>>>> https://github.com/wso2/jaggery/blob/master/components/script-engine/org.jaggeryjs.scriptengine/pom.xml
>>>>
>>>> Added maven dependencies for Jackson data-bind and annotations
>>>>
>>>>
>>>> I have tried to build(mvn clean install) Jaggery and product-Jaggery
>>>> respectively.I can build Jaggery successfully But It has failed when
>>>> building product Jaggery at Jaggery - Profile Generation.
>>>>
>>>> As per offline discussion with Niranjan I have added <importBundleDef>
>>>> and <*dependency*> for Jackson data-bind and annotation in POM.xml[
>>>> https://github.com/wso2/jaggery/blob/master/features/org.jaggeryjs.server.feature/pom.xml]
>>>> file as below.
>>>>
>>>> <importBundleDef>com.fasterxml.jackson.core:jackson-databind</importBundleDef>
>>>> <importBundleDef>com.fasterxml.jackson.core:jackson-annotations</importBundleDef>
>>>>
>>>>
>>>> <dependency>
>>>>     <groupId>com.fasterxml.jackson.core</groupId>
>>>>     <artifactId>jackson-databind</artifactId>
>>>> </dependency>
>>>> <dependency>
>>>>     <groupId>com.fasterxml.jackson.core</groupId>
>>>>     <artifactId>jackson-annotations</artifactId>
>>>> </dependency>
>>>>
>>>>
>>>> But Still I am getting product Jaggery build fail at the point Jaggery - 
>>>> Profile Generation,following error can be observe in the command line.
>>>>
>>>>
>>>> Installation failed.Cannot complete the install because one or more 
>>>> required items could not be found.
>>>>
>>>>  Software being installed: uuid Module - Feature 1.4.3 
>>>> (org.jaggeryjs.modules.uuid.feature.group 1.4.3)
>>>>  Missing requirement: jackson-databind 2.0.0 (jackson-databind 2.0.0) 
>>>> requires 'package com.fasterxml.jackson.core [2.0.0,3.0.0)' but it could 
>>>> not be found
>>>>  Cannot satisfy dependency:
>>>>   From: org.jaggeryjs.modules.uuid 1.4.3 (org.jaggeryjs.modules.uuid 1.4.3)
>>>>   To: package org.jaggeryjs.scriptengine.exceptions [0.11.0,1.0.0)
>>>>  Cannot satisfy dependency:
>>>>
>>>>   From: uuid Module - Feature 1.4.3 
>>>> (org.jaggeryjs.modules.uuid.feature.group 1.4.3)
>>>>   To: org.jaggeryjs.modules.uuid [1.4.3]
>>>>  Cannot satisfy dependency:
>>>>   From: org.jaggeryjs.scriptengine 0.12.1.SNAPSHOT 
>>>> (org.jaggeryjs.scriptengine 0.12.1.SNAPSHOT)
>>>>   To: package com.fasterxml.jackson.databind [2.0.0,3.0.0)
>>>> Application failed, log file location: 
>>>> /home/rajee/.m2/repository/org/eclipse/tycho/tycho-p2-runtime/0.13.0/eclipse/configuration/1454469448601.log
>>>>
>>>>
>>>> Can anyone help me to figure out the cause of build fail.Hope I have 
>>>> included all the steps I have followed.Your help would be appreciated.
>>>>
>>>> As per the error, the build is failing because jackson-databind
>>> requires com.fasterxml.jackson.core within the range 2.0.0 and 3.0.0. If
>>> this is required by the feature then you need to bundle it in the feature.
>>> Shall we have a look at this today?
>>>
>>>>
>>>>
>>>>
>>>> On Tue, Feb 2, 2016 at 10:23 AM, Rajeenthini Satkunam <
>>>> [email protected]> wrote:
>>>>
>>>>> Hi sajith,
>>>>>
>>>>> Noted and thank you for the suggestions.
>>>>>
>>>>> On Mon, Feb 1, 2016 at 11:53 PM, Sajith Ariyarathna <[email protected]
>>>>> > wrote:
>>>>>
>>>>>> Hi Rajeenthini,
>>>>>>
>>>>>> You are hoping to add the following code snippet to the serializeJSON
>>>>>> method [1] of the org.jaggeryjs.scriptengine.util.HostObjectUtil
>>>>>> class.
>>>>>>
>>>>>>>     String JsonString = null;
>>>>>>>     try{
>>>>>>>         ObjectMapper mapper = new ObjectMapper();
>>>>>>>         JsonString = mapper.writeValueAsString(obj);
>>>>>>>     }catch (Exception e){
>>>>>>>         System.out.println(e.getMessage());
>>>>>>>     }
>>>>>>>
>>>>>>>     return "{\"javaObject\":\"true\",\"object\":\"" + JsonString + 
>>>>>>> "\"}";
>>>>>>>
>>>>>>> There are few issue in this code snippet.
>>>>>>
>>>>>>    - Don't catch generic Exception, always catch a specific
>>>>>>    exception. In here you should catch JsonProcessingException [2].
>>>>>>    - Don't use System.out.println to log exceptions. Instead use a
>>>>>>    proper logger. HostObjectUtil class has a log object defined in
>>>>>>    line 30 [3], you can use that.
>>>>>>    - Returning '{"javaObject": "true", "object": "{ ... }"}' string
>>>>>>    gives a wrong impression that there are two attributes called 
>>>>>> 'javaObject'
>>>>>>    & 'object' in the logged object. I think just returning the JSON 
>>>>>> string
>>>>>>    will be enough.
>>>>>>
>>>>>> After corrections (I also took the liberty to reduce no of lines):
>>>>>>
>>>>>> try {
>>>>>>
>>>>>> return (new ObjectMapper()).writeValueAsString(obj);
>>>>>>
>>>>>> } catch (JsonProcessingException e) {
>>>>>>
>>>>>> log.debug("Object " + obj.toString() + " of class " +
>>>>>> obj.getClassName() + " cannot be converted to JSON");
>>>>>>
>>>>>> }
>>>>>>
>>>>>> return "{}";
>>>>>>
>>>>>>
>>>>>> [1]
>>>>>> https://github.com/wso2/jaggery/blob/master/components/script-engine/org.jaggeryjs.scriptengine/src/main/java/org/jaggeryjs/scriptengine/util/HostObjectUtil.java#L78
>>>>>> [2]
>>>>>> https://static.javadoc.io/com.fasterxml.jackson.core/jackson-databind/2.6.3/com/fasterxml/jackson/databind/ObjectMapper.html#writeValueAsString(java.lang.Object)
>>>>>> [3]
>>>>>> https://github.com/wso2/jaggery/blob/master/components/script-engine/org.jaggeryjs.scriptengine/src/main/java/org/jaggeryjs/scriptengine/util/HostObjectUtil.java#L30
>>>>>>
>>>>>> Thanks.
>>>>>>
>>>>>>
>>>>>> On Mon, Feb 1, 2016 at 8:13 PM, Rajeenthini Satkunam <
>>>>>> [email protected]> wrote:
>>>>>>
>>>>>>> Hi sajith,
>>>>>>>
>>>>>>> i have added this Student class for example.As for current
>>>>>>> implementation goes like this way in Jaggery below.
>>>>>>>
>>>>>>> public static String serializeJSON(Object obj) {
>>>>>>>     if (obj instanceof Wrapper) {
>>>>>>>         obj = ((Wrapper) obj).unwrap();
>>>>>>>     }
>>>>>>>     if (obj == null) {
>>>>>>>         return "null";
>>>>>>>     }
>>>>>>>     if (obj instanceof Undefined) {
>>>>>>>         return "null";
>>>>>>>     }
>>>>>>>     if (obj instanceof Boolean) {
>>>>>>>         return Boolean.toString((Boolean) obj);
>>>>>>>     }
>>>>>>>     if (obj instanceof String) {
>>>>>>>         return serializeString((String) obj);
>>>>>>>     }
>>>>>>>     if (obj instanceof ConsString) {
>>>>>>>         return serializeString(obj.toString());
>>>>>>>     }
>>>>>>>     if (obj instanceof Number) {
>>>>>>>         return obj.toString();
>>>>>>>     }
>>>>>>>     if (obj instanceof XMLObject) {
>>>>>>>         return serializeString(serializeXML((ScriptableObject) obj));
>>>>>>>     }
>>>>>>>     if (obj instanceof NativeObject) {
>>>>>>>         return serializeNativeObject((NativeObject) obj);
>>>>>>>     }
>>>>>>>     if (obj instanceof NativeArray) {
>>>>>>>         return serializeNativeArray((NativeArray) obj);
>>>>>>>     }
>>>>>>>     if (obj instanceof Object[]) {
>>>>>>>         return serializeObjectArray((Object[]) obj);
>>>>>>>     }
>>>>>>>     if (obj instanceof Scriptable) {
>>>>>>>         Scriptable object = (Scriptable) obj;
>>>>>>>         String jsClass = object.getClassName();
>>>>>>>         if ("Date".equals(jsClass)) {
>>>>>>>             return serializeString(serializeNativeDate(object));
>>>>>>>         } else if ("Error".equals(jsClass)) {
>>>>>>>             return serializeString(serializeNativeError(object));
>>>>>>>         }
>>>>>>>     }
>>>>>>>     String JsonString = null;
>>>>>>>     try{
>>>>>>>         ObjectMapper mapper = new ObjectMapper();
>>>>>>>         JsonString = mapper.writeValueAsString(obj);
>>>>>>>     }catch (Exception e){
>>>>>>>         System.out.println(e.getMessage());
>>>>>>>     }
>>>>>>>
>>>>>>>     return "{\"javaObject\":\"true\",\"object\":\"" + JsonString + 
>>>>>>> "\"}";
>>>>>>> }
>>>>>>>
>>>>>>> AFAIU since we are parsing Object type of argument It is better we
>>>>>>> can use ObjectMapper.Correct me If I am wrong.Your suggestion would be 
>>>>>>> more
>>>>>>> appreciated.
>>>>>>>
>>>>>>> On Sun, Jan 31, 2016 at 10:23 AM, Sajith Ariyarathna <
>>>>>>> [email protected]> wrote:
>>>>>>>
>>>>>>>> Hi Rajeenthini,
>>>>>>>>
>>>>>>>> AFAIK ability to convert a POJO into JSON object does nor affect
>>>>>>>> the output of a Jaggery Log of that POJO.
>>>>>>>> To get a proper log output for a Java object, you need to implement
>>>>>>>> the "toString" method in that Java class. For example in your case,
>>>>>>>>
>>>>>>>> public class Student {
>>>>>>>>
>>>>>>>> ...
>>>>>>>>
>>>>>>>> public String toString() {
>>>>>>>>
>>>>>>>> return "{name: " + this.name + ", age: " + this.age + ", indexNo:
>>>>>>>> " + this.indexNo + "}";
>>>>>>>>
>>>>>>>> }
>>>>>>>>
>>>>>>>> }
>>>>>>>>
>>>>>>>> Now you can log a Student object in your Jaggery code as following.
>>>>>>>>
>>>>>>>> log.info(studentObj.toString());
>>>>>>>>
>>>>>>>>
>>>>>>>> Thanks.
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> On Sat, Jan 30, 2016 at 9:28 PM, Rajeenthini Satkunam <
>>>>>>>> [email protected]> wrote:
>>>>>>>>
>>>>>>>>> Hi,
>>>>>>>>>
>>>>>>>>> I have worked on writing Jaggery test for Jaggery-Product.I can
>>>>>>>>> observe when we tried to log the object it always gives us empty JSON
>>>>>>>>> ({}).Currently the implementation goes this way when we try to log 
>>>>>>>>> java
>>>>>>>>> object.
>>>>>>>>>
>>>>>>>>> So I was trying to get rid of this observed behavior of Jaggery.I
>>>>>>>>> have gone through a solution.
>>>>>>>>>
>>>>>>>>> *using Jackson[1] *
>>>>>>>>>
>>>>>>>>> We can convert java object to JSON[2] and can log it.
>>>>>>>>>
>>>>>>>>> I have shared the piece of code I have tried out with Jackson
>>>>>>>>> below.
>>>>>>>>>
>>>>>>>>> *Simple POJO Student class*
>>>>>>>>>
>>>>>>>>> public class Student {
>>>>>>>>>     private int age;
>>>>>>>>>     private String name;
>>>>>>>>>     private String indexNo;
>>>>>>>>>
>>>>>>>>>     public int getAge() {
>>>>>>>>>         return age;
>>>>>>>>>     }
>>>>>>>>>
>>>>>>>>>     public void setAge(int age) {
>>>>>>>>>         this.age = age;
>>>>>>>>>     }
>>>>>>>>>
>>>>>>>>>     public String getName() {
>>>>>>>>>         return name;
>>>>>>>>>     }
>>>>>>>>>
>>>>>>>>>     public void setName(String name) {
>>>>>>>>>         this.name = name;
>>>>>>>>>     }
>>>>>>>>>
>>>>>>>>>     public String getIndexNo() {
>>>>>>>>>         return indexNo;
>>>>>>>>>     }
>>>>>>>>>
>>>>>>>>>     public void setIndexNo(String indexNo) {
>>>>>>>>>         this.indexNo = indexNo;
>>>>>>>>>     }
>>>>>>>>> }
>>>>>>>>>
>>>>>>>>> *Simple class to test Jackson*
>>>>>>>>>
>>>>>>>>> import com.fasterxml.jackson.databind.ObjectMapper;
>>>>>>>>> import util.Student;
>>>>>>>>> public class MyTest {
>>>>>>>>>     public static void main(String args[]){
>>>>>>>>>         Student st = new Student();
>>>>>>>>>         st.setIndexNo("DS001");
>>>>>>>>>         st.setAge(12);
>>>>>>>>>         st.setName("kareena");
>>>>>>>>>         try{
>>>>>>>>>             ObjectMapper mapper = new ObjectMapper();
>>>>>>>>>             String jsonInString = mapper.writeValueAsString(st);
>>>>>>>>>             
>>>>>>>>> System.out.println("************************************************");
>>>>>>>>>             System.out.println(jsonInString);
>>>>>>>>>             
>>>>>>>>> System.out.println("************************************************");
>>>>>>>>>
>>>>>>>>>         }catch(Exception e){
>>>>>>>>>             System.out.print("Exception caught  "+ e);
>>>>>>>>>         }
>>>>>>>>>
>>>>>>>>>     }
>>>>>>>>> }
>>>>>>>>>
>>>>>>>>> Actual output by above code.
>>>>>>>>> ************************************************
>>>>>>>>> {"age":12,"name":"kareena","indexNo":"DS001"}
>>>>>>>>> ************************************************
>>>>>>>>>
>>>>>>>>> I have added a dependency in the POM.xml
>>>>>>>>>
>>>>>>>>> <dependency>
>>>>>>>>>     <groupId>com.fasterxml.jackson.core</groupId>
>>>>>>>>>     <artifactId>jackson-databind</artifactId>
>>>>>>>>>     <version>2.6.3</version>
>>>>>>>>> </dependency>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> IMHO we can use the same scenario in case of log JAVA object.Your
>>>>>>>>> help and suggestions are more appreciated and guide me if I am wrong 
>>>>>>>>> or I
>>>>>>>>> can have better solution than this.
>>>>>>>>>
>>>>>>>>> [1] - https://github.com/FasterXML/jackson-databind
>>>>>>>>> [2] -
>>>>>>>>> http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
>>>>>>>>> --
>>>>>>>>>
>>>>>>>>> *Thank You.*
>>>>>>>>>
>>>>>>>>> *Rajeenthini Satkunam*
>>>>>>>>>
>>>>>>>>> *Associate Software Engineer | WSO2*
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> *E:[email protected] <[email protected]>*
>>>>>>>>>
>>>>>>>>> *M :+94770832823 <%2B94770832823>   *
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> _______________________________________________
>>>>>>>>> Dev mailing list
>>>>>>>>> [email protected]
>>>>>>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> Sajith Ariyarathna
>>>>>>>> Software Engineer; WSO2, Inc.;  http://wso2.com/
>>>>>>>> mobile: +94 77 6602284, +94 71 3951048
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>>
>>>>>>> *Thank You.*
>>>>>>>
>>>>>>> *Rajeenthini Satkunam*
>>>>>>>
>>>>>>> *Associate Software Engineer | WSO2*
>>>>>>>
>>>>>>>
>>>>>>> *E:[email protected] <[email protected]>*
>>>>>>>
>>>>>>> *M :+94770832823 <%2B94770832823>   *
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Sajith Ariyarathna
>>>>>> Software Engineer; WSO2, Inc.;  http://wso2.com/
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>>
>>>>> *Thank You.*
>>>>>
>>>>> *Rajeenthini Satkunam*
>>>>>
>>>>> *Associate Software Engineer | WSO2*
>>>>>
>>>>>
>>>>> *E:[email protected] <[email protected]>*
>>>>>
>>>>> *M :+94770832823 <%2B94770832823>   *
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>>
>>>> *Thank You.*
>>>>
>>>> *Rajeenthini Satkunam*
>>>>
>>>> *Associate Software Engineer | WSO2*
>>>>
>>>>
>>>> *E:[email protected] <[email protected]>*
>>>>
>>>> *M :+94770832823 <%2B94770832823>   *
>>>>
>>>>
>>>
>>> Regards,
>>> Nira
>>>
>>> --
>>>
>>> *Niranjan Karunanandham*
>>> Senior Software Engineer - WSO2 Inc.
>>> WSO2 Inc.: http://www.wso2.com
>>>
>>
>>
>>
>> --
>>
>> *Thank You.*
>>
>> *Rajeenthini Satkunam*
>>
>> *Associate Software Engineer | WSO2*
>>
>>
>> *E:[email protected] <[email protected]>*
>>
>> *M :+94770832823 <%2B94770832823>   *
>>
>>
>
>
> --
>
> *Thank You.*
>
> *Rajeenthini Satkunam*
>
> *Associate Software Engineer | WSO2*
>
>
> *E:[email protected] <[email protected]>*
>
> *M :+94770832823 <%2B94770832823>   *
>
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 
Sajith Ariyarathna
Software Engineer; WSO2, Inc.;  http://wso2.com/
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to