Coder Social home page Coder Social logo

log-file's Introduction

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.

log-file's People

Contributors

xxlabaza avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

jobop

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.