Skip to content

Commit

Permalink
file logger
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkdev committed Jul 11, 2023
0 parents commit bda5777
Show file tree
Hide file tree
Showing 13 changed files with 649 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.v]
indent_style = tab
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* text=auto eol=lf
*.bat eol=crlf

**/*.v linguist-language=V
**/*.vv linguist-language=V
**/*.vsh linguist-language=V
**/v.mod linguist-language=V
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Binaries for programs and plugins
main
*.exe
*.exe~
*.so
*.dylib
*.dll

logs/
*.log

# Ignore binary output folders
bin/

# Ignore common editor/system specific metadata
.DS_Store
.idea/
.vscode/
*.iml

# ENV
.env

# vweb and database
*.db
*.js
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2023 github.com/dnkdev/logx

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 changes: 50 additions & 0 deletions examples/test_prebuilt.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module main

import log
import time
//import logx.prebuilt.filesize.filelog
import logx.prebuilt.daily.filelog


fn start_vlog(repeats int) string{
mut vlog := log.Log{}
vlog.set_level(.debug)
vlog.set_full_logpath('./debug_vlog.log')
bench := time.ticks()
for i in 0..repeats {
vlog.debug('Some text ${i}')
vlog.info('Some text ${i}')
vlog.warn('Some text ${i}')
vlog.error('Some text ${i}')
}
return 'vLog Time: ${time.ticks() - bench}ms'
}

fn start_logx(repeats int)! string {
mut logger := filelog.new()!
logger.set_level(.trace)
dump(logger)
bench := time.ticks()
for i in 0..repeats {
logger.trace('Some text ${i}')
logger.debug('Some text ${i}')
logger.info('Some text ${i}')
logger.note('Some text ${i}')
}
return 'logX Time: ${time.ticks() - bench}ms'
}

fn main(){
mut repeats := 10000
txt_logx := start_logx(repeats)!
txt_vlog := start_vlog(repeats)


C.atexit(
fn[txt_logx, txt_vlog](){
println(txt_vlog)
println(txt_logx)
}
)
//time.sleep(2000*time.millisecond)
}
107 changes: 107 additions & 0 deletions prebuilt/daily/filelog/filelog.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
module filelog

import logx

[rotation: 'daily']
pub struct PrebuiltFileLog {
pub mut:
trace_ logx.LevelInfo [cap: 10000; file: 'logs/trace/trace.log']
debug_ logx.LevelInfo [cap: 9000; file: 'logs/debug/debug.log']
info_ logx.LevelInfo [cap: 8000; file: 'logs/info/info.log']
note_ logx.LevelInfo [cap: 7000; file: 'logs/note/note.log']
warn_ logx.LevelInfo [cap: 1000; file: 'logs/warn/warn.log']
alert_ logx.LevelInfo [cap: 1000; file: 'logs/alert/alert.log']
error_ logx.LevelInfo [cap: 1000; file: 'logs/error/error.log']
fatal_ logx.LevelInfo [cap: 1; file: 'logs/fatal/fatal.log']
log_level int
log_day int // should be if [rotation: 'daily'] set
}

pub enum LogLevel {
trace
debug
info
note
warn
alert
error
fatal
}

pub fn (mut l PrebuiltFileLog) wait() {
logx.wait(mut l)
}

pub fn (mut l PrebuiltFileLog) set_level(level LogLevel) {
l.log_level = int(level)
}

pub fn (mut l PrebuiltFileLog) trace(s string) {
if l.log_level > int(LogLevel.trace) {
return
}
logx.check_daily_rotation(mut l)
l.trace_.ch <- l.trace_.formatter(s, 'TRACE')
}

pub fn (mut l PrebuiltFileLog) debug(s string) {
if l.log_level > int(LogLevel.debug) {
return
}
logx.check_daily_rotation(mut l)
l.debug_.ch <- l.debug_.formatter(s, 'DEBUG')
}

pub fn (mut l PrebuiltFileLog) info(s string) {
if l.log_level > int(LogLevel.info) {
return
}
logx.check_daily_rotation(mut l)
l.info_.ch <- l.info_.formatter(s, 'INFO ')
}

pub fn (mut l PrebuiltFileLog) note(s string) {
if l.log_level > int(LogLevel.note) {
return
}
logx.check_daily_rotation(mut l)
l.note_.ch <- l.note_.formatter(s, 'NOTE ')
}

pub fn (mut l PrebuiltFileLog) warn(s string) {
if l.log_level > int(LogLevel.warn) {
return
}
logx.check_daily_rotation(mut l)
l.warn_.ch <- l.warn_.formatter(s, 'WARN ')
}

pub fn (mut l PrebuiltFileLog) alert(s string) {
if l.log_level > int(LogLevel.alert) {
return
}
logx.check_daily_rotation(mut l)
l.alert_.ch <- l.alert_.formatter(s, 'ALERT')
}

pub fn (mut l PrebuiltFileLog) error(s string) {
if l.log_level > int(LogLevel.error) {
return
}
logx.check_daily_rotation(mut l)
l.error_.ch <- l.error_.formatter(s, 'ERROR')
}

pub fn (mut l PrebuiltFileLog) fatal(s string) {
if l.log_level > int(LogLevel.fatal) {
return
}
logx.check_daily_rotation(mut l)
l.fatal_.ch <- l.fatal_.formatter(s, 'FATAL')
logx.wait(mut l)
panic(s)
}

pub fn new() !&PrebuiltFileLog {
return logx.from_new(PrebuiltFileLog{})!
}
106 changes: 106 additions & 0 deletions prebuilt/filesize/filelog/filelog.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
module filelog

import logx

[rotation: 'filesize']
pub struct PrebuiltFileLog {
pub mut:
trace_ logx.LevelInfo [max_size: '1MB'; cap: 10000; file: 'logs/trace/trace.log']
debug_ logx.LevelInfo [max_size: '1MB'; cap: 9000; file: 'logs/debug/debug.log']
info_ logx.LevelInfo [max_size: '1MB'; cap: 8000; file: 'logs/info/info.log']
note_ logx.LevelInfo [max_size: '1MB'; cap: 7000; file: 'logs/note/note.log']
warn_ logx.LevelInfo [max_size: '1MB'; cap: 1000; file: 'logs/warn/warn.log']
alert_ logx.LevelInfo [max_size: '1MB'; cap: 1000; file: 'logs/alert/alert.log']
error_ logx.LevelInfo [max_size: '1MB'; cap: 1000; file: 'logs/error/error.log']
fatal_ logx.LevelInfo [max_size: '1MB'; cap: 1; file: 'logs/fatal/fatal.log']
log_level int
}

pub enum LogLevel {
trace
debug
info
note
warn
alert
error
fatal
}

pub fn (mut l PrebuiltFileLog) wait() {
logx.wait(mut l)
}

pub fn (mut l PrebuiltFileLog) set_level(level LogLevel) {
l.log_level = int(level)
}

pub fn (mut l PrebuiltFileLog) trace(s string) {
if l.log_level > int(LogLevel.trace) {
return
}
logx.check_level_size_rotation(mut l.trace_)
l.trace_.ch <- l.trace_.formatter(s, 'TRACE')
}

pub fn (mut l PrebuiltFileLog) debug(s string) {
if l.log_level > int(LogLevel.debug) {
return
}
logx.check_level_size_rotation(mut l.debug_)
l.debug_.ch <- l.debug_.formatter(s, 'DEBUG')
}

pub fn (mut l PrebuiltFileLog) info(s string) {
if l.log_level > int(LogLevel.info) {
return
}
logx.check_level_size_rotation(mut l.info_)
l.info_.ch <- l.info_.formatter(s, 'INFO ')
}

pub fn (mut l PrebuiltFileLog) note(s string) {
if l.log_level > int(LogLevel.note) {
return
}
logx.check_level_size_rotation(mut l.note_)
l.note_.ch <- l.note_.formatter(s, 'NOTE ')
}

pub fn (mut l PrebuiltFileLog) warn(s string) {
if l.log_level > int(LogLevel.warn) {
return
}
logx.check_level_size_rotation(mut l.warn_)
l.warn_.ch <- l.warn_.formatter(s, 'WARN ')
}

pub fn (mut l PrebuiltFileLog) alert(s string) {
if l.log_level > int(LogLevel.alert) {
return
}
logx.check_level_size_rotation(mut l.alert_)
l.alert_.ch <- l.alert_.formatter(s, 'ALERT')
}

pub fn (mut l PrebuiltFileLog) error(s string) {
if l.log_level > int(LogLevel.error) {
return
}
logx.check_level_size_rotation(mut l.error_)
l.error_.ch <- l.error_.formatter(s, 'ERROR')
}

pub fn (mut l PrebuiltFileLog) fatal(s string) {
if l.log_level > int(LogLevel.fatal) {
return
}
logx.check_level_size_rotation(mut l.fatal_)
l.fatal_.ch <- l.fatal_.formatter(s, 'FATAL')
logx.wait(mut l)
panic(s)
}

pub fn new() !&PrebuiltFileLog {
return logx.from_new(PrebuiltFileLog{})!
}
35 changes: 35 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 📕 logx

My small library for easier custom logging implementation. File logging.<br>
- [x] One or multiple files / directories
- [x] daily rotation
- [x] filesize rotation

Example implementation (daily rotation, filesize rotation): look at code:
<a href="https://github.com/dnkdev/logx/tree/master/prebuilt">filelog</a><br>
Daily : `import logx.prebuilt.daily.filelog` <br>
Filesize: `import logx.prebuilt.filesize.filelog`<br>

```v
import logx.prebuilt.size.filelog
//...
mut logger := filelog.new()!
logger.set_level(.trace)
logger.trace('Some text')
logger.debug('Some text')
logger.info('Some text')
logger.note('Some text')
logger.warn('Some text')
logger.alert('Some text')
logger.error('Some text')
logger.fatal('Some text') // panic
logger.wait() // call wait if the program can exit before all writes are done
```

##

(log)X == eXperimental

##
Loading

0 comments on commit bda5777

Please sign in to comment.