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

com.undefinedlabs.scope.logger.FileScopeLogger Maven / Gradle / Ivy

package com.undefinedlabs.scope.logger;

import com.undefinedlabs.scope.settings.ScopeSettings;
import com.undefinedlabs.scope.statistics.Statistics;
import com.undefinedlabs.scope.utils.props.SystemProps;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Calendar;

public class FileScopeLogger extends AbstractScopeLogger {

  private static final String FILE_SEPARATOR =
      java.nio.file.FileSystems.getDefault().getSeparator();

  private final Path loggerPath;

  FileScopeLogger(final ScopeSettings settings) {
    super(settings);
    loggerPath = ScopeLoggerUtils.isScopeTestsRunning() ? null : buildScopeLoggerPath(settings);
  }

  protected static Path buildScopeLoggerPath(final ScopeSettings settings) {
    final String rootPathStr = FileScopeLoggerRootPathResolver.INSTANCE.resolveRootPath(settings);
    final String currentDateTime = DF.format(Calendar.getInstance().getTime());
    final String filename =
        String.format(
            "scope-%s-%s-%s-%s.log",
            SystemProps.AGENT_TYPE,
            settings.getSetting(ScopeSettings.SCOPE_COMMIT_SHA),
            currentDateTime,
            SystemProps.AGENT_ID);
    final String filepath = String.format("%s" + FILE_SEPARATOR + "%s", rootPathStr, filename);
    final Path rootPathDirectory = Paths.get(rootPathStr);
    final Path logPathFile = Paths.get(filepath);

    try {
      if (Files.notExists(rootPathDirectory)) {
        Files.createDirectories(rootPathDirectory);
      }

      if (Files.notExists(logPathFile)) {
        Files.createFile(logPathFile);
      }

      Statistics.INSTANCE.registerLogPathFile(logPathFile.toString());
      return logPathFile;
    } catch (final IOException e) {
      System.err.println(
          "-- [SCOPE] Error creating FileLog path. AccessDeniedException " + e.getMessage());
      return null;
    }
  }

  protected void log(final Path path, final String message) {
    if (path == null) {
      return;
    }

    try (PrintWriter writer =
        new PrintWriter(
            new BufferedWriter(
                new OutputStreamWriter(
                    new FileOutputStream(path.toFile(), true), StandardCharsets.UTF_8)))) {
      writer.println(ScopeLoggerUtils.INSTANCE.formatMessage(message));

      if (writer.checkError()) {
        System.err.println("-- [SCOPE] Error in PrintWriter!");
      }

    } catch (final Exception e) {
      System.err.format("Exception: %s%n", e);
    }
  }

  @Override
  protected void log(final String message) {
    log(loggerPath, message);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy