lumbermill.File Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lumbermill-core Show documentation
Show all versions of lumbermill-core Show documentation
Where Logs are cut into Lumber
The newest version!
/*
* Copyright 2016 Sony Mobile Communications, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 lumbermill;
import lumbermill.api.Codec;
import lumbermill.api.Codecs;
import lumbermill.api.Event;
import lumbermill.api.JsonEvent;
import lumbermill.internal.MapWrap;
import lumbermill.internal.Streams;
import lumbermill.internal.StringTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class File {
private static final Logger LOGGER = LoggerFactory.getLogger(File.class);
File(){}
/**
* Reads each line of the file as an Event, specify codec to decide how to parse each line.
* {@code flatMap (
* file (
* file : '/path/to/file.txt', // Supports templating '{path}'
* codec : Codecs.jsonObject() // Optional, default is Codecs.textToJson()
* ))}
*/
public Func1> lines(Map map) {
MapWrap config = MapWrap.of(map).assertExists("file");
StringTemplate fileTemplate = config.asStringTemplate("file");
Codec codec = config.getObject("codec", (Codec)Codecs.TEXT_TO_JSON);
return event -> {
String file = fileTemplate.format(event).get();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Reading each line of file {} with codec {}", file, codec);
}
try (Stream lines = Streams.lines(file)) {
List allLines = lines.map(line -> codec.from(line))
.collect(toList());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Finished reading file {} with {} lines", file, allLines.size());
}
return Observable.from(allLines);
}
};
}
public Observable readFileAsLines(Map map) {
return Observable.just(Codecs.TEXT_TO_JSON.from("{}")).flatMap(lines(map));
}
public Observable readFile(Map map) {
MapWrap config = MapWrap.of(map).assertExists("file");
String file = config.asString("file");
Codec codec = config.getObject("codec", (Codec)Codecs.BYTES);
return Observable.just(codec.from(Streams.read(new java.io.File(file))));
}
}