投稿者: miztaka, カテゴリ: MySQL, tags: mysql
[mysql] MySQLのバイナリログ削除 はコメントを受け付けていません
appengineのアプリケーションログをチェックして、ERRORレベルのログがあった場合、管理者にメールで通知するプログラムを作ってみました。
Servletで作ってcronで実行します。
public class LogWatchServlet extends HttpServlet {
private final static Log log = LogFactory.getLog(LogWatchServlet.class);
private final static long duration = 20 * 60 * 1000; // 20min
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone.setDefault(TimeZone.getTimeZone("GMT+9:00"));
LogQuery query = LogQuery.Builder
.withDefaults()
.minLogLevel(LogLevel.ERROR)
.majorVersionIds(Arrays.asList(new String[]{SystemProperty.applicationVersion.get().split("\\.")[0]}))
.includeAppLogs(true)
.startTimeMillis(System.currentTimeMillis() - duration);
log.debug("log watch version: " + SystemProperty.applicationVersion.get());
// ログ取得
String lastOffset = null;
List<RequestLogs> logs = new ArrayList<RequestLogs>();
do {
if (lastOffset != null) {
query.offset(lastOffset);
}
int count = 0;
for (RequestLogs record : LogServiceFactory.getLogService().fetch(query)) {
logs.add(record);
lastOffset = record.getOffset();
count++;
}
if (count == 0) {
break;
}
} while(true);
// ログがあったらメール送信
if (! logs.isEmpty()) {
List<String> logbuffer = new ArrayList<String>();
int i=0;
for (RequestLogs record: logs) {
logbuffer.add(format.format(new Date(record.getEndTimeUsec()/1000)) + " " + record.getResource());
for (AppLogLine line: record.getAppLogLines()) {
if (line.getLogLevel().compareTo(LogLevel.ERROR) >= 0) {
logbuffer.add(" " + line.getLogMessage());
}
}
if (i > 50) {
break;
}
i++;
}
logbuffer.add("\n\n全" + logs.size() + "件");
// メール送信
MailService mailService = MailServiceFactory.getMailService();
String sender = "admin@" + AppConst.appId() + ".appspotmail.com";
String subject = "[" + AppConst.appId() + "] applicaton log notification";
MailService.Message message = new MailService.Message(sender, null, subject, StringUtils.join(logbuffer, "\n"));
mailService.sendToAdmins(message);
}
return;
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
[appengine] アプリケーションログをチェックしてメールで通知する はコメントを受け付けていません
これでいいのかしら。。
public static Key allocateId(AsyncDatastoreService ds, String kind)
throws NullPointerException {
if (ds == null) {
throw new NullPointerException("The ds parameter must not be null.");
}
if (kind == null) {
throw new NullPointerException(
"The kind parameter must not be null.");
}
String ns = NamespaceManager.get();
if (ns == null) {
ns = "";
}
String cacheKey = "!"+ns+":"+kind;
Iterator<Key> keys = keysCache.get(cacheKey);
if (keys != null && keys.hasNext()) {
return keys.next();
}
keys =
FutureUtil
.getQuietly(allocateIdsAsync(ds, kind, KEY_CACHE_SIZE))
.iterator();
keysCache.put(cacheKey, keys);
return keys.next();
}
[appengine][slim3] DatastoreUtil#allocateIdがNamespaceに対応するように修正 はコメントを受け付けていません
listプロパティにInMemoryInCriterionを適用するとエラーになるので修正。
これでいいのかしら。。
public boolean accept(Object model) {
Object v = convertValueForDatastore(attributeMeta.getValue(model));
for (Object o : value) {
if (v instanceof Collection<?>) {
for (Object v2: (Collection<?>)v) {
if (compareValue(v2, o) == 0) {
return true;
}
}
} else {
if (compareValue(v, o) == 0) {
return true;
}
}
}
return false;
}
[appengine][slim3]InMemoryInCriterionがlistプロパティでも動作するように修正 はコメントを受け付けていません
忘れがちなのでメモ。
cd appengine-java-sdk-xxx\bin
appcfg.cmd vacuum_indexes c:\path\to\eclipse\project\war
[appengine]不要なインデックスを削除するコマンド はコメントを受け付けていません