Expand description
EZLog
🦀️Rust 移动端开发体验EZLog is a high efficiency Cross-platform logging library.
it is inspired by Xlog and Loagan, rewrite in Rust.
Feature
- multi platform: Flutter, Android, iOS, Windows, Linux, MacOS
- map file into memory by mmap.
- zlib compression.
- AEAD encryption.
- fetch log by callback.
- trim out of date files.
- CLI paser support.
Quick Start
Flutter
see Flutter project README.md
iOS
By CocoaPods
Add EZLog to Podfile
pod 'EZLog', '~> 0.1'then
pod updateOpen Xcode, add sample code
import EZLog
init() {
pthread_setname_np("main")
#if DEBUG
ezlogInitWithTrace()
#else
ezlogInit()
#endif
let dirPath = URL.documents.appendingPathComponent("ezlog").relativePath
let config = EZLogConfig(level: Level.trace,
dirPath: dirPath,
name: "demo",
keepDays: 7,
maxSize: 150*1024,
compress: CompressKind.ZLIB,
compressLevel: CompressLevel.DEFAULT,
cipher: Cipher.AES128GCM,
cipherKey: [UInt8]("a secret key!!!!".utf8),
cipherNonce: [UInt8]("unique nonce".utf8))
let logger = EZLogger(config: config)
ezlogRegisterCallback(success: {name, date, logs in
if !logs.isEmpty {
for log in logs {
print("name:" + name + " date:" + date + " log:" + log);
}
} else {
print("no log found at that time")
}
}, fail: {name, date, err in
print("name:" + name + " date:" + date + " err:" + err);
})
logger.debug("first blood")
}- click run and see console ouput.
Android
Add ezlog to dependencies
open top-level build.gradle, add mavenCentral to repositories.
buildscript {
repositories {
...
mavenCentral()
...
}
}
allprojects {
repositories {
...
mavenCentral()
...
}
}open app level build.gradle, add ezlog
dependencies {
implementation "wtf.s1.ezlog:ezlog:0.1.7"
}sync gradle
Setup in application. For example
override fun onCreate() {
super.onCreate()
val path = File(filesDir, "ezlog").absolutePath
val config = EZLogConfig.Builder("demo", path)
.compress(EZLog.CompressZlib)
.compressLevel(EZLog.CompressFast)
.cipher(EZLog.Aes128Gcm)
.cipherKey("a secret key!!!!".toByteArray())
.cipherNonce("unique nonce".toByteArray())
.enableTrace(BuildConfig.DEBUG)
.build()
EZLog.initWith(config)
EZLog.v("ezlog", "first blood")
EZLog._registerCallback(object : Callback {
override fun onLogsFetchSuccess(
logName: String?,
date: String?,
logs: Array<out String>?
) {
Log.i("ezlog", "$logName $date ${logs.contentToString()}")
logs?.let {
logs.getOrNull(0)?.let { log ->
Log.i("ezlog", "check file exists ${File(log).exists()}")
}
}
}
override fun onLogsFetchFail(logName: String?, date: String?, err: String?) {
Log.i("ezlog", "$logName $date $err")
}
})
}
Build from source code
install and config rustcurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/envuse rust nightly
rustup default nightly-2022-08-10we use build-std feature, so add nightly src component
rustup component add rust-src --toolchain nightly-x86_64-apple-darwinadd target: iOS, android, etc…
rustup target add aarch64-linux-android armv7-linux-androideabi aarch64-apple-ios aarch64-apple-ios-sim x86_64-apple-iosclone repository and open in command line tool. then run
cargo checkwait crates download…
cargo build -p ezlogFor android build
we use cargo-ndk to build dylib
cargo install cargo-ndkcd android
sh b_android.shthen open current workspace in AndroidStudio
For iOS build
install cbindgen
cargo install --force cbindgencd ios dir
sh b_ios.shopen the ios/EZlog.xcworkspace in Xcode
License
See LICENSE-MIT, LICENSE-APACHE,
Macros
Every important log case make an event.
if you care about what’s things going on, just register an event listener.
Structs
A config to set up EZLogger
The builder of EZLogConfig
The Logger struct to implement the Log encode.
Single Log record
EZRecord’s builder
Default Event implementation, print every event in console
Fetch Logs file‘s path reqeust
Fetch Logs file‘s path result.
EZLog file Header
Enums
Cipher kind current support
Compress type can be used to compress the log file.
Compress level
Log level, used to filter log records
Log version enum
Constants
A EZLogger default name. current is “default”.
Log file fixed header length.
Traits
The Compression trait + Decompression trait
Compress function abstract
The Encryptor trait + Decryptor trait
Decompress function abstract
decrypt function abstract
Async callback for fetch log files
Encrypt function abstract
EZLog Event Listener
Functions
Create a new EZLogger from an EZLogConfig
Force flush the log file
Flush all log files
Init ezlog
Request logs file path array at the date which EZLogger’s name is define in the parameter
Set the boxed EZLogCallback
Set global Event listener