All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.base.io.DataFileLog Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/*
 * Copyright 2013 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.way.base.io;

import static com.google.common.collect.Lists.newArrayList;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Joiner;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;

/**
 * @author [email protected] (Marcio Endo)
 */
public class DataFileLog {

  private static final String SEPARATOR = System.getProperty("line.separator");
  private static final Logger logger = LoggerFactory.getLogger(DataFileLog.class);

  private final DataFileMeta meta;
  private final List log;

  private DataFileLog(DataFileMeta meta, List log) {
    this.meta = meta;
    this.log = newArrayList(log);
  }

  public static DataFileLog of(DataFile file) {
    DataFileMeta meta = file.metaOf("log");

    List log;
    try {
      log = meta.readLines();
    } catch (IOException e) {
      log = ImmutableList.of();
    }

    return new DataFileLog(file, log);
  }

  public void debug(String format, Object... args) {
    String msg = msg("DEBUG", format, args);
    logger.debug(msg);
  }

  public void info(String format, Object... args) {
    String msg = msg("INFO", format, args);
    logger.info(msg);
  }

  public void warn(String format, Object... args) {
    String msg = msg("WARN", format, args);
    logger.warn(msg);
  }

  public void error(String format, Object... args) {
    String msg = msg("ERROR", format, args);
    logger.error(msg);
  }

  public void error(String format, Throwable e, Object... args) {
    String msg = msg("ERROR", format, args);
    logger.error(msg, e);
    String trace = Throwables.getStackTraceAsString(e);
    log.add(trace);
  }

  public List getLog() {
    return log;
  }

  public synchronized void flush() {
    try {
      String text = Joiner.on(SEPARATOR).join(log);
      meta.write(text);
    } catch (IOException e) {

    }
  }

  @Override
  public String toString() {
    return log.toString();
  }

  private String msg(String level, String format, Object... args) {
    String msg = String.format(format, args);
    logAndFlush(level, msg);
    return msg;
  }

  private void logAndFlush(String level, String message) {
    String prefix = String.format("[%1$s] %2$tY-%2$tm-%2$td %2$tH:%2$tM:%2$tS", level, new Date());
    String msg = prefix + " " + message;

    log.add(msg);
    flushIfNecessary();
  }

  private void flushIfNecessary() {
    int size = log.size();
    if (size % 5 == 0) {
      flush();
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy