谷歌应用引擎。堆垛机。使用Java进行日志记录
我想将日志发布到Stackdriver的“自定义日志”中。这些特性是beta版的,因此可能没有说明如何在appengine上使用javaapi进行日志记录。无论如何,我想描述我的问题:我使用此API版本:谷歌应用引擎。堆垛机。使用Java进行日志记录,java,google-app-engine,logging,stackdriver,Java,Google App Engine,Logging,Stackdriver,我想将日志发布到Stackdriver的“自定义日志”中。这些特性是beta版的,因此可能没有说明如何在appengine上使用javaapi进行日志记录。无论如何,我想描述我的问题:我使用此API版本: "com.google.apis:google-api-services-logging:v2beta1-rev10-1.21.0" 因此,首先,我像这样构建日志对象(我希望这是正确的): 获取日志客户端后,我尝试将一个条目推送到日志: LogEntry lEntry = new L
"com.google.apis:google-api-services-logging:v2beta1-rev10-1.21.0"
因此,首先,我像这样构建日志对象(我希望这是正确的):
获取日志客户端后,我尝试将一个条目推送到日志:
LogEntry lEntry = new LogEntry();
lEntry.setTextPayload("I want to see this log!");
WriteLogEntriesRequest writeLogEntriesRequest = new WriteLogEntriesRequest();
writeLogEntriesRequest.setLogName("My Super log");
List<LogEntry> listEntries = new ArrayList<>();
listEntries.add(lEntry);
writeLogEntriesRequest.setEntries(listEntries);
Logging logging = LoggingManager.createAuthorizedClient();
Write write = logging.entries().write(writeLogEntriesRequest);
WriteLogEntriesResponse writeLogResponse = write.execute();
==更新:工作解决方案===
多亏了沙玛小姐。下面是完整的代码,如何将数据发送到日志:
public boolean send() {
WriteLogEntriesResponse response = null;
try {
final String now = getNowUtc();
final String insertId = "entry-at-" + now;
final Map<String, String> labels = ImmutableMap.of("project_id", SharedConstants.APPLICATION_ID, "name",
"projects/" + SharedConstants.APPLICATION_ID + "/logs/" + this.logName);
Logging service = createAuthorizedClient();
MonitoredResource ressource = new MonitoredResource();
ressource.setType("logging_log");
ressource.setLabels(labels);
LogEntry entry = new LogEntry().setInsertId(insertId).setResource(ressource).setTimestamp(now)
.setJsonPayload(this.entriesMap)
.setLogName("projects/" + SharedConstants.APPLICATION_ID + "/logs/" + this.logName)
.setSeverity(this.severity);
WriteLogEntriesRequest content = (new WriteLogEntriesRequest())
.setEntries(Collections.singletonList(entry));
response = service.entries().write(content).execute();
} catch (Exception e) {
}
return response != null;
}
private static String getNowUtc() {
SimpleDateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormatUtc.format(new Date());
}
公共布尔发送(){
WriteLogEntriesResponse响应=null;
试一试{
现在最后一个字符串=getNowUtc();
最后一个字符串insertId=“entry at-”+现在;
final Map labels=ImmutableMap.of(“project_id”,SharedConstants.APPLICATION_id,“name”,
“projects/”+SharedConstants.APPLICATION_ID+“/logs/”+this.logName);
日志服务=createAuthorizedClient();
MonitoredResource ressource=新MonitoredResource();
setType(“logging_log”);
ressource.setLabels(标签);
LogEntry entry=new LogEntry().setInsertId(insertId).setResource(ressource).setTimestamp(现在)
.setJsonPayload(this.entriemap)
.setLogName(“projects/”+SharedConstants.APPLICATION_ID+“/logs/”+this.logName)
.setSeverity(该严重性);
WriteLogEntriesRequest内容=(新的WriteLogEntriesRequest())
.setEntries(Collections.singletonList(条目));
response=service.entries().write(content.execute();
}捕获(例外e){
}
返回响应!=null;
}
私有静态字符串getNowUtc(){
SimpleDataFormat UTC=新的SimpleDataFormat(“yyyy-MM-dd'T'HH:MM:ss.SSS'Z'”;
dateFormatUtc.setTimeZone(TimeZone.getTimeZone(“UTC”);
return dateFormatUtc.format(new Date());
}
此代码与最新版本的日志api配合使用效果良好
因此,EntriesMap是:
private Map<String, Object> entriesMap;
私有地图中心地图;
我在非托管Python环境中遇到了相同的问题。我已经开始工作了,我可以在你的代码中看到至少两个问题
public boolean send() {
WriteLogEntriesResponse response = null;
try {
final String now = getNowUtc();
final String insertId = "entry-at-" + now;
final Map<String, String> labels = ImmutableMap.of("project_id", SharedConstants.APPLICATION_ID, "name",
"projects/" + SharedConstants.APPLICATION_ID + "/logs/" + this.logName);
Logging service = createAuthorizedClient();
MonitoredResource ressource = new MonitoredResource();
ressource.setType("logging_log");
ressource.setLabels(labels);
LogEntry entry = new LogEntry().setInsertId(insertId).setResource(ressource).setTimestamp(now)
.setJsonPayload(this.entriesMap)
.setLogName("projects/" + SharedConstants.APPLICATION_ID + "/logs/" + this.logName)
.setSeverity(this.severity);
WriteLogEntriesRequest content = (new WriteLogEntriesRequest())
.setEntries(Collections.singletonList(entry));
response = service.entries().write(content).execute();
} catch (Exception e) {
}
return response != null;
}
private static String getNowUtc() {
SimpleDateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormatUtc.format(new Date());
}
private Map<String, Object> entriesMap;