如何将 log4mongo 文档映射到 java 对象

发布于 2024-12-27 11:37:57 字数 2322 浏览 0 评论 0原文

基本上我的 log4mongo 正在记录用户活动,我想选择 一个用户并查看他的日志条目,

此代码工作正常,

BasicDBObject blogEntryDbObj = (BasicDBObject) 
mongo.getDB().getCollection("log").findOne(new BasicDBObject("_id", 
new ObjectId(blogEntryId))); 

但在这里我收到错误“尝试映射到未映射的类:

log = morphia.fromDBObject(log.class, blogEntryDbObj); 

日志名称应该是什么” > 类或者也许是那个 是无关紧要的。我意识到我的 POJO 可能没有正确设置
但我不知道出了什么问题。

log4mongo 文档如下所示。

{ 
  "_id" : ObjectId("4eecf4e4f9cb90f308193614"), 
  "timestamp" : new Date("Sat, 17 Dec 2011 21:00:36 GMT +01:00"), 
  "level" : "INFO", 
  "thread" : "Server.Connection:146.53.243.167:4570", 
  "message" : "setting RegistrationId for: zambe", 
  "loggerName" : { 
    "fullyQualifiedClassName" : "zambe.afa8cc37-4796-49c1- 
a175-05a8daf0a88c", 
    "package" : ["zambe", "afa8cc37-4796-49c1-a175-05a8daf0a88c"], 
    "className" : "afa8cc37-4796-49c1-a175-05a8daf0a88c" 
  }, 
  "fileName" : "PushUpdateRegistrationId.java", 
  "method" : "connect", 
  "lineNumber" : "61", 
  "class" : { 
    "fullyQualifiedClassName" : 
"com.carlsberg.server.PushUpdateRegistrationId", 
    "package" : ["com", "carlsberg", "server", 
"PushUpdateRegistrationId"], 
    "className" : "PushUpdateRegistrationId" 
  }, 
  "host" : { 
    "process" : "5832@ERIKSWEDMINI", 
    "name" : "ERIKSWEDMINI", 
    "ip" : "192.168.1.150" 
  } 
}

这是我试图映射到的 POJO

@Entity(value = "log") 
public class log 
{ 
    @Id private ObjectId id; 
    Date timestamp; 
    Level level; 
    String thread;
    String message; 

    @Embedded  
    LoggerName loggerName; 

    String fileName; 
    String method; 
    String lineNumber; 

    @Embedded("class") 
    LoggerClass loggerClass; 

    @Embedded
    MyHost host; 
} 

@Entity(noClassnameStored=true) 
@Embedded 
class LoggerName 
{ 
    String fullyQualifiedClassName; 
    @Property("package") 
    String[] _package; 
    String className; 

} 

@Entity(noClassnameStored=true) 
@Embedded 
class LoggerClass 
{ 
    String fullyQualifiedClassName; 
    @Property("package") 
    String[] _package; 
    String className; 

} 

@Entity(noClassnameStored=true) 
@Embedded 
class MyHost 
{ 
    String process; 
    String name;
    String ip;
} 

Basically my log4mongo is logging user activity and i want to select
one user and view his log entries,

This code is working ok

BasicDBObject blogEntryDbObj = (BasicDBObject) 
mongo.getDB().getCollection("log").findOne(new BasicDBObject("_id", 
new ObjectId(blogEntryId))); 

but here im getting the error "Trying to map to an unmapped class:

log = morphia.fromDBObject(log.class, blogEntryDbObj); 

What should the package name be for the log class or maybe that
is irrelevant. I realize that probably my POJO is not correctly set up
but i cannot figure out what's wrong.

The log4mongo document look like this.

{ 
  "_id" : ObjectId("4eecf4e4f9cb90f308193614"), 
  "timestamp" : new Date("Sat, 17 Dec 2011 21:00:36 GMT +01:00"), 
  "level" : "INFO", 
  "thread" : "Server.Connection:146.53.243.167:4570", 
  "message" : "setting RegistrationId for: zambe", 
  "loggerName" : { 
    "fullyQualifiedClassName" : "zambe.afa8cc37-4796-49c1- 
a175-05a8daf0a88c", 
    "package" : ["zambe", "afa8cc37-4796-49c1-a175-05a8daf0a88c"], 
    "className" : "afa8cc37-4796-49c1-a175-05a8daf0a88c" 
  }, 
  "fileName" : "PushUpdateRegistrationId.java", 
  "method" : "connect", 
  "lineNumber" : "61", 
  "class" : { 
    "fullyQualifiedClassName" : 
"com.carlsberg.server.PushUpdateRegistrationId", 
    "package" : ["com", "carlsberg", "server", 
"PushUpdateRegistrationId"], 
    "className" : "PushUpdateRegistrationId" 
  }, 
  "host" : { 
    "process" : "5832@ERIKSWEDMINI", 
    "name" : "ERIKSWEDMINI", 
    "ip" : "192.168.1.150" 
  } 
}

Here is my POJO im trying to map to

@Entity(value = "log") 
public class log 
{ 
    @Id private ObjectId id; 
    Date timestamp; 
    Level level; 
    String thread;
    String message; 

    @Embedded  
    LoggerName loggerName; 

    String fileName; 
    String method; 
    String lineNumber; 

    @Embedded("class") 
    LoggerClass loggerClass; 

    @Embedded
    MyHost host; 
} 

@Entity(noClassnameStored=true) 
@Embedded 
class LoggerName 
{ 
    String fullyQualifiedClassName; 
    @Property("package") 
    String[] _package; 
    String className; 

} 

@Entity(noClassnameStored=true) 
@Embedded 
class LoggerClass 
{ 
    String fullyQualifiedClassName; 
    @Property("package") 
    String[] _package; 
    String className; 

} 

@Entity(noClassnameStored=true) 
@Embedded 
class MyHost 
{ 
    String process; 
    String name;
    String ip;
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心 2025-01-03 11:37:57

morphia.map 是实例方法。您需要在 Morphia 的同一个实例上 morphia.map 才能使 morphia.fromDBObject 正常工作

morphia.map is instance method. You need to morphia.map it on the same instance of Morphia to get morphia.fromDBObject to work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文