Last active
December 11, 2015 20:18
-
-
Save hito-asa/4653890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void fetchAndWriteLog(LogQuery query, String fileName) throws Exception { | |
FileService fs = FileServiceFactory.getFileService(); | |
AppEngineFile file = null; | |
FileWriteChannel channel = null; | |
Writer writer = null; | |
try { | |
long start = System.currentTimeMillis(); | |
file = fs.createNewGSFile(new GSFileOptionsBuilder() | |
.setBucket(GS_BUCKET_NAME) | |
.setKey(fileName) | |
.setMimeType("text/plain") | |
.setAcl("project-private") | |
.build()); | |
for (RequestLogs record : LogServiceFactory.getLogService().fetch(query)) { | |
if (!"GET".equalsIgnoreCase(record.getMethod()) && !"POST".equalsIgnoreCase(record.getMethod())) | |
continue; | |
// 15秒に1回closeしてopenする(Cloud Storageの制約回避) | |
if ((System.currentTimeMillis() - start) > 15000) { | |
start = System.currentTimeMillis(); | |
writer.close(); | |
channel.close(); | |
Thread.sleep(1000); | |
} | |
if (channel == null || !channel.isOpen()) { | |
channel = fs.openWriteChannel(file, true); | |
writer = new BufferedWriter(Channels.newWriter(channel, "UTF8"), GS_WRITER_BUFFER_SIZE); | |
} | |
for (AppLogLine appLog : record.getAppLogLines()) { | |
String line = appLog.getLogMessage(); | |
if (line.contains(ActivityLogger.STDLOG_PREFIX)) { | |
String message = line.split(ActivityLogger.STDLOG_PREFIX)[1]; | |
writer.write(message); | |
} | |
} | |
} | |
} catch (Exception e) { | |
logger.warning("failed to logging cloud storage."); | |
throw e; | |
} finally { | |
try { | |
if (writer != null) | |
writer.close(); | |
if (channel != null) | |
channel.closeFinally(); | |
} catch (Exception e) { | |
logger.warning("failed to close cloud storage io channel."); | |
e.printStackTrace(System.out); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment