Log file

A Java library for working with append-only files

License

License

GroupId

GroupId

com.xxlabaza.utils
ArtifactId

ArtifactId

log-file
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Log file
A Java library for working with append-only files
Project URL

Project URL

https://github.com/xxlabaza/log-file
Source Code Management

Source Code Management

https://github.com/xxlabaza/log-file

Download log-file

How to add to project

<!-- https://jarcasting.com/artifacts/com.xxlabaza.utils/log-file/ -->
<dependency>
    <groupId>com.xxlabaza.utils</groupId>
    <artifactId>log-file</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.xxlabaza.utils/log-file/
implementation 'com.xxlabaza.utils:log-file:1.0.0'
// https://jarcasting.com/artifacts/com.xxlabaza.utils/log-file/
implementation ("com.xxlabaza.utils:log-file:1.0.0")
'com.xxlabaza.utils:log-file:jar:1.0.0'
<dependency org="com.xxlabaza.utils" name="log-file" rev="1.0.0">
  <artifact name="log-file" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.xxlabaza.utils', module='log-file', version='1.0.0')
)
libraryDependencies += "com.xxlabaza.utils" % "log-file" % "1.0.0"
[com.xxlabaza.utils/log-file "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
io.appulse : utils-java jar 1.16.4

provided (4)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.18.10
net.jcip : jcip-annotations jar 1.0
com.github.spotbugs : spotbugs-annotations jar 3.1.12
com.google.code.findbugs : jsr305 jar 3.0.2

test (5)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-engine jar 5.5.2
org.junit.jupiter : junit-jupiter-params jar 5.5.2
org.assertj : assertj-core jar 3.14.0
org.mockito : mockito-core jar 3.1.0
org.mockito : mockito-junit-jupiter jar 3.1.0

Project Modules

There are no modules declared in this project.

Overview log-file

build_status maven_central License

A Java library for working with append-only files (also known as the log files). The main methods of such files are:

  • append - for appending arbitary data;
  • load - for fully file read from the begining till the end.

Usage example

import java.nio.file.Paths;

import com.xxlabaza.utils.log.file.Config;
import com.xxlabaza.utils.log.file.LogFile;

import io.appulse.utils.Bytes;
import io.appulse.utils.HexUtil;


// let's instantiate a log file:
Config config = Config.builder()
    .path(Paths.get("./my.log"))
    .blockBufferSizeBytes(32)
    .forceFlush(false)
    .build();

LogFile logFile = new LogFile(config);


// than, write some data to log file
Bytes record = Bytes.resizableArray()
    .write4B(42);

logFile.append(record);


// now, read all records from the file and print them to STDOUT
logFile.load((buffer, position) -> {
  String dump = HexUtil.prettyHexDump(buffer);
  System.out.println(dump);
  return true; // true - continue reading, false - stop reading
});


// flush and close the file
logFile.close();

Under the hood

A log file has the next structure:

log file structure
file header blocks
version block size block 0 ... block n
1 byte 4 bytes blocks count * block size

File header

File header's description:

version
The file's format version, which tells the features set is used in the file.
block size
The size of a block buffer, in bytes, which is used in the current file.

Block

Each block consist of the records set:

block
record 1 ... record n

The records have the following structure:

record
checksum type body length body
4 bytes 1 byte 2 bytes body length
checksum
The checksum value, which is calculated from type, body length and body values.
type
One of the record's types:
  • FULL - the data is presented entirely in this record;
  • FIRST - it is the first chunk of data;
  • MIDDLE - one of the middle parts of data;
  • LAST - the last piece of data.
body length
The length of the next body section, in bytes.
body
the record's payload.

Versions

Version
1.0.0