Skip to content

Commit

Permalink
Replace a global static logger with a local logger (#2001)
Browse files Browse the repository at this point in the history
Replace a global static logger with a local logger

So we have now better traces of which exact class is called.
  • Loading branch information
dadoonet authored Jan 17, 2025
1 parent 543b2dd commit 18a717e
Show file tree
Hide file tree
Showing 80 changed files with 317 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package fr.pilato.elasticsearch.crawler.fs.beans;

import fr.pilato.elasticsearch.crawler.fs.test.framework.AbstractFSCrawlerTestCase;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;

import java.io.IOException;
Expand All @@ -30,7 +32,7 @@
import static org.hamcrest.Matchers.is;

public class FsJobParserTest extends AbstractFSCrawlerTestCase {

private static final Logger logger = LogManager.getLogger();
private static final FsJob JOB_EMPTY = FsJob.builder().build();

private void jobTester(FsJob source) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
Expand All @@ -35,7 +36,7 @@
*/
public class BootstrapChecks {

private static final Logger logger = LogManager.getLogger(BootstrapChecks.class);
private static final Logger logger = LogManager.getLogger();

public static void check() {
checkJvm();
Expand Down Expand Up @@ -64,7 +65,7 @@ static Percentage computePercentage(ByteSizeValue current, ByteSizeValue max) {
}

private static void checkUTF8() {
String encoding = System.getProperty("file.encoding");
String encoding = Charset.defaultCharset().displayName();
if (!encoding.equals(StandardCharsets.UTF_8.name())) {
logger.warn("[file.encoding] should be [{}] but is [{}]", StandardCharsets.UTF_8.name(), encoding);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class FsCrawlerCli {

private static final long CLOSE_POLLING_WAIT_MS = 100;

private static final Logger logger = LogManager.getLogger(FsCrawlerCli.class);
private static final Logger logger = LogManager.getLogger();
private static FsCrawlerPluginsManager pluginsManager;

@SuppressWarnings("CanBeFinal")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

public class FsCrawlerJobsUtil {

private static final Logger logger = LogManager.getLogger(FsCrawlerJobsUtil.class);
private static final Logger logger = LogManager.getLogger();

public static List<String> listExistingJobs(Path configDir) {
List<String> files = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import fr.pilato.elasticsearch.crawler.fs.settings.FsSettingsFileHandler;
import fr.pilato.elasticsearch.crawler.fs.settings.Server;
import fr.pilato.elasticsearch.crawler.fs.test.framework.AbstractFSCrawlerTestCase;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -41,7 +43,7 @@
* We want to test FSCrawler main app
*/
public class FsCrawlerCliDefaultSettingsTest extends AbstractFSCrawlerTestCase {

private static final Logger logger = LogManager.getLogger();
private static Path metadataDir;

@BeforeClass
Expand All @@ -52,7 +54,7 @@ public static void createFsCrawlerJobDir() throws IOException {
Files.createDirectory(metadataDir);
}
copyDefaultResources(metadataDir);
staticLogger.debug(" --> Test metadata dir ready in [{}]", metadataDir);
logger.debug(" --> Test metadata dir ready in [{}]", metadataDir);
}

@AfterClass
Expand All @@ -61,14 +63,14 @@ public static void printMetadataDirContent() throws IOException {
}

private static void printLs(Path dir) throws IOException {
staticLogger.debug("ls -l {}", dir);
logger.debug("ls -l {}", dir);
Files.list(dir).forEach(path -> {
if (Files.isDirectory(path)) {
try {
printLs(path);
} catch (IOException ignored) { }
} else {
staticLogger.debug("{}", path);
logger.debug("{}", path);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import fr.pilato.elasticsearch.crawler.fs.test.framework.AbstractFSCrawlerTestCase;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
Expand All @@ -47,7 +48,7 @@
* We want to test FSCrawler main app
*/
public class FsCrawlerCliLoggerTest extends AbstractFSCrawlerTestCase {

private static final Logger logger = LogManager.getLogger();
private static Path metadataDir;

@BeforeClass
Expand All @@ -58,7 +59,7 @@ public static void createFsCrawlerJobDir() throws IOException {
Files.createDirectory(metadataDir);
}
copyDefaultResources(metadataDir);
staticLogger.debug(" --> Test metadata dir ready in [{}]", metadataDir);
logger.debug(" --> Test metadata dir ready in [{}]", metadataDir);
}

@AfterClass
Expand All @@ -67,14 +68,14 @@ public static void printMetadataDirContent() throws IOException {
}

private static void printLs(Path dir) throws IOException {
staticLogger.debug("ls -l {}", dir);
logger.debug("ls -l {}", dir);
Files.list(dir).forEach(path -> {
if (Files.isDirectory(path)) {
try {
printLs(path);
} catch (IOException ignored) { }
} else {
staticLogger.debug("{}", path);
logger.debug("{}", path);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import fr.pilato.elasticsearch.crawler.fs.settings.FsSettings;
import fr.pilato.elasticsearch.crawler.fs.settings.FsSettingsFileHandler;
import fr.pilato.elasticsearch.crawler.fs.test.framework.AbstractFSCrawlerTestCase;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -42,7 +44,7 @@
* We want to test FSCrawler main app
*/
public class FsCrawlerCliTest extends AbstractFSCrawlerTestCase {

private static final Logger logger = LogManager.getLogger();
private static Path metadataDir;

@BeforeClass
Expand All @@ -53,7 +55,7 @@ public static void createFsCrawlerJobDir() throws IOException {
Files.createDirectory(metadataDir);
}
copyDefaultResources(metadataDir);
staticLogger.debug(" --> Test metadata dir ready in [{}]", metadataDir);
logger.debug(" --> Test metadata dir ready in [{}]", metadataDir);
}

@AfterClass
Expand All @@ -62,14 +64,14 @@ public static void printMetadataDirContent() throws IOException {
}

private static void printLs(Path dir) throws IOException {
staticLogger.debug("ls -l {}", dir);
logger.debug("ls -l {}", dir);
Files.list(dir).forEach(path -> {
if (Files.isDirectory(path)) {
try {
printLs(path);
} catch (IOException ignored) { }
} else {
staticLogger.debug("{}", path);
logger.debug("{}", path);
}
});
}
Expand Down Expand Up @@ -104,7 +106,7 @@ public void testWithWrongSettingsFile() throws Exception {

Path jobDir = metadataDir.resolve(jobName);
Files.createDirectories(jobDir);
Files.writeString(jobDir.resolve(SETTINGS_YAML), "" +
Files.writeString(jobDir.resolve(SETTINGS_YAML),
"name: \"test\"\n" +
"fs:\n" +
" url: \"/path/to/docs\"\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class FsCrawlerImpl implements AutoCloseable {
@Deprecated
public static final String INDEX_TYPE_FOLDER = "folder";

private static final Logger logger = LogManager.getLogger(FsCrawlerImpl.class);
private static final Logger logger = LogManager.getLogger();

public static final int LOOP_INFINITE = -1;
public static final long MAX_SLEEP_RETRY_TIME = TimeValue.timeValueSeconds(30).millis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import static fr.pilato.elasticsearch.crawler.fs.framework.JsonUtil.asMap;

public abstract class FsParserAbstract extends FsParser {
private static final Logger logger = LogManager.getLogger(FsParserAbstract.class);
private static final Logger logger = LogManager.getLogger();

private static final String FSCRAWLER_IGNORE_FILENAME = ".fscrawlerignore";

Expand Down Expand Up @@ -224,7 +224,7 @@ private void updateFsJob(String jobName, LocalDateTime scanDate) throws Exceptio
// We need to round that latest date to the lower second and
// remove 2 seconds.
// See #82: https://github.com/dadoonet/fscrawler/issues/82
scanDate = scanDate.minus(2, ChronoUnit.SECONDS);
scanDate = scanDate.minusSeconds(2);
FsJob fsJob = FsJob.builder()
.setName(jobName)
.setLastrun(scanDate)
Expand Down Expand Up @@ -523,7 +523,7 @@ private String read(InputStream input) throws IOException {
* @param id id of the folder
* @param folder path object
*/
private void indexDirectory(String id, Folder folder) throws IOException {
private void indexDirectory(String id, Folder folder) {
if (!closed) {
managementService.storeVisitedDirectory(fsSettings.getElasticsearch().getIndexFolder(), id, folder);
} else {
Expand Down Expand Up @@ -575,7 +575,7 @@ private void removeEsDirectoryRecursively(final String path) throws Exception {
/**
* Remove a document with the document service
*/
private void esDelete(FsCrawlerDocumentService service, String index, String id) throws IOException {
private void esDelete(FsCrawlerDocumentService service, String index, String id) {
logger.debug("Deleting {}/{}", index, id);
if (!closed) {
service.delete(index, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.logging.log4j.Logger;

public class FsParserNoop extends FsParser {
private static final Logger logger = LogManager.getLogger(FsParserNoop.class);
private static final Logger logger = LogManager.getLogger();
private final FsSettings fsSettings;

public FsParserNoop(FsSettings fsSettings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

public class FsCrawlerDocumentServiceElasticsearchImpl implements FsCrawlerDocumentService {

private static final Logger logger = LogManager.getLogger(FsCrawlerDocumentServiceElasticsearchImpl.class);
private static final Logger logger = LogManager.getLogger();

private final IElasticsearchClient client;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

public class FsCrawlerManagementServiceElasticsearchImpl implements FsCrawlerManagementService {

private static final Logger logger = LogManager.getLogger(FsCrawlerManagementServiceElasticsearchImpl.class);
private static final Logger logger = LogManager.getLogger();

// TODO Optimize it. We can probably use a search for a big array of filenames instead of
// searching fo 10000 files (which is somehow limited).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -45,7 +44,7 @@
import static fr.pilato.elasticsearch.crawler.fs.framework.FsCrawlerUtil.getOwnerName;

public class FileAbstractorFile extends FileAbstractor<File> {
private final Logger logger = LogManager.getLogger(FileAbstractorFile.class);
private static final Logger logger = LogManager.getLogger();

public FileAbstractorFile(FsSettings fsSettings) {
super(fsSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.logging.log4j.Logger;

public class FTPUtils {
private static final Logger logger = LogManager.getLogger(FTPUtils.class);
private static final Logger logger = LogManager.getLogger();

/**
* Determines FTPFile permissions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import java.util.stream.Collectors;

public class FileAbstractorFTP extends FileAbstractor<FTPFile> {
private final Logger logger = LogManager.getLogger(FileAbstractorFTP.class);
private static final Logger logger = LogManager.getLogger();

private FTPClient ftp;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
Expand All @@ -47,6 +49,7 @@
import org.mockftpserver.fake.filesystem.UnixFakeFileSystem;

public class FileAbstractorFTPTest extends AbstractFSCrawlerTestCase {
private static final Logger logger = LogManager.getLogger();
private FakeFtpServer fakeFtpServer;
private final String nestedDir = "/nested";
private final String permissionDir = "/permission";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import java.util.stream.StreamSupport;

public class FileAbstractorSSH extends FileAbstractor<SftpClient.DirEntry> {
private final Logger logger = LogManager.getLogger(FileAbstractorSSH.class);
private static final Logger logger = LogManager.getLogger();

private final FsCrawlerSshClient fsCrawlerSshClient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@
import java.security.KeyPair;

public class FsCrawlerSshClient implements AutoCloseable {
private final Logger logger = LogManager.getLogger(FsCrawlerSshClient.class);
private static final Logger logger = LogManager.getLogger();

private final String host;
private final int port;
private final String username;
private final String password;
private final String pemPath;

private ClientSession session;
private SshClient sshClient;
private SftpClient sftpClient;

Expand All @@ -53,8 +52,7 @@ public FsCrawlerSshClient(String username, String password, String pemPath, Stri

public void open() throws Exception {
sshClient = createSshClient();
session = openSshSession(sshClient, username, password, pemPath, host, port);
sftpClient = createSftpClient(session);
sftpClient = createSftpClient(openSshSession(sshClient, username, password, pemPath, host, port));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import fr.pilato.elasticsearch.crawler.fs.settings.FsSettings;
import fr.pilato.elasticsearch.crawler.fs.settings.Server;
import fr.pilato.elasticsearch.crawler.fs.test.framework.AbstractFSCrawlerTestCase;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyPairResourceWriter;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.config.keys.AuthorizedKeysAuthenticator;
Expand Down Expand Up @@ -52,12 +54,10 @@
import static org.hamcrest.Matchers.*;

public class FileAbstractorSSHTest extends AbstractFSCrawlerTestCase {

private static final Logger logger = LogManager.getLogger();
private final static String SSH_USERNAME = "USERNAME";
private final static String SSH_PASSWORD = "PASSWORD";

private SshServer sshd = null;

Path testDir = rootTmpDir.resolve("test-ssh");

@Before
Expand Down
Loading

0 comments on commit 18a717e

Please sign in to comment.