mirror of
https://github.com/gechandesu/structlog.git
synced 2026-01-23 16:04:14 +03:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f688b3cad5 | |||
| 8b13a596f7 | |||
| 98738e773d | |||
| 791b376ff3 |
48
.github/workflows/docs.yaml
vendored
Normal file
48
.github/workflows/docs.yaml
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
name: Docs
|
||||
on:
|
||||
push:
|
||||
branches: [ "master" ]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup V
|
||||
run: |
|
||||
wget -qO /tmp/v.zip https://github.com/vlang/v/releases/latest/download/v_linux.zip
|
||||
unzip -q /tmp/v.zip -d /tmp
|
||||
echo /tmp/v >> "$GITHUB_PATH"
|
||||
|
||||
- name: Build docs
|
||||
run: |
|
||||
v doc -f html -m .
|
||||
pushd _docs
|
||||
ln -vs ${{ github.event.repository.name }}.html index.html
|
||||
ls -alFh
|
||||
popd
|
||||
|
||||
- name: Upload static files as artifact
|
||||
id: deployment
|
||||
uses: actions/upload-pages-artifact@v3
|
||||
with:
|
||||
path: _docs/
|
||||
|
||||
deploy:
|
||||
needs: build
|
||||
environment:
|
||||
name: github-pages
|
||||
url: ${{ steps.deployment.outputs.page_url }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Deploy to GitHub Pages
|
||||
id: deployment
|
||||
uses: actions/deploy-pages@v4
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
pages: write
|
||||
id-token: write
|
||||
27
.github/workflows/test.yaml
vendored
Normal file
27
.github/workflows/test.yaml
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
name: Lint and test
|
||||
on:
|
||||
push:
|
||||
branches: [ "master" ]
|
||||
pull_request:
|
||||
branches: [ "master" ]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup V
|
||||
run: |
|
||||
wget -qO /tmp/v.zip https://github.com/vlang/v/releases/latest/download/v_linux.zip
|
||||
unzip -q /tmp/v.zip -d /tmp
|
||||
echo /tmp/v >> "$GITHUB_PATH"
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
v fmt -verify .
|
||||
v vet -v -W -I -F -r .
|
||||
v missdoc -r --verify .
|
||||
v -stats test .
|
||||
@@ -4,8 +4,10 @@ import structlog
|
||||
fn main() {
|
||||
// Initialize logger with edited timestamp.
|
||||
log := structlog.new(
|
||||
timestamp_format: .unix
|
||||
handler: structlog.JSONHandler{
|
||||
timestamp: structlog.Timestamp{
|
||||
format: .unix
|
||||
}
|
||||
handler: structlog.JSONHandler{
|
||||
writer: os.stdout()
|
||||
}
|
||||
)
|
||||
|
||||
53
structlog.v
53
structlog.v
@@ -96,9 +96,8 @@ pub fn (r Record) append(field ...Field) Record {
|
||||
mut fields_orig := unsafe { r.fields }
|
||||
fields_orig << field
|
||||
return Record{
|
||||
channel: r.channel
|
||||
level: r.level
|
||||
fields: &fields_orig
|
||||
...r
|
||||
fields: &fields_orig
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,9 +109,8 @@ pub fn (r Record) prepend(field ...Field) Record {
|
||||
mut new_fields := unsafe { field }
|
||||
new_fields << r.fields
|
||||
return Record{
|
||||
channel: r.channel
|
||||
level: r.level
|
||||
fields: new_fields
|
||||
...r
|
||||
fields: new_fields
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +119,7 @@ pub fn (r Record) field(name string, value Value) Record {
|
||||
return r.append(Field{ name: name, value: value })
|
||||
}
|
||||
|
||||
// field adds new message field to a record and returns the modified record.
|
||||
// message adds new message field to a record and returns the modified record.
|
||||
// This is a shothand for `field('message', 'message text')`.
|
||||
pub fn (r Record) message(s string) Record {
|
||||
return r.field('message', s)
|
||||
@@ -144,6 +142,25 @@ pub fn (r Record) send() {
|
||||
r.channel <- r
|
||||
}
|
||||
|
||||
pub struct Timestamp {
|
||||
pub mut:
|
||||
// format sets the format of datettime for logs.
|
||||
// TimestampFormat values map 1-to-1 to the date formats provided by `time.Time`.
|
||||
// If .custom format is selected the timestamp_custom field must be set.
|
||||
format TimestampFormat = .rfc3339
|
||||
|
||||
// custom sets the custom datetime string format if format is set to .custom.
|
||||
// See docs for Time.format_custom() fn from stadnard `time` module.
|
||||
custom string
|
||||
|
||||
// If local is true the local time will be used instead of UTC.
|
||||
local bool
|
||||
}
|
||||
|
||||
fn (t Timestamp) as_value() Value {
|
||||
return timestamp(t.format, t.custom, t.local)
|
||||
}
|
||||
|
||||
pub enum TimestampFormat {
|
||||
default
|
||||
rfc3339
|
||||
@@ -167,20 +184,23 @@ pub:
|
||||
// This value cannot be changed after logger initialization.
|
||||
level Level = .info
|
||||
|
||||
// timestamp holds the timestamp settings.
|
||||
timestamp Timestamp
|
||||
|
||||
add_level bool = true // if true add `level` field to all log records.
|
||||
add_timestamp bool = true // if true add `timestamp` field to all log records.
|
||||
|
||||
// timestamp_format sets the format of datettime for logs.
|
||||
// TimestampFormat values map 1-to-1 to the date formats provided by `time.Time`.
|
||||
// If .custom format is selected the timestamp_custom field must be set.
|
||||
timestamp_format TimestampFormat = .rfc3339
|
||||
timestamp_format TimestampFormat = .rfc3339 @[deprecated: 'use `timestamp` instead']
|
||||
|
||||
// timestamp_custom sets the custom datetime string format if timestapm_format is
|
||||
// set to .custom. See docs for Time.format_custom() fn from stadnard `time` module.
|
||||
timestamp_custom string
|
||||
timestamp_custom string @[deprecated: 'use `timestamp` instead']
|
||||
|
||||
// If timestamp_local is true the local time will be used instead of UTC.
|
||||
timestamp_local bool
|
||||
timestamp_local bool @[deprecated: 'use `timestamp` instead']
|
||||
|
||||
// handler holds a log record handler object which is used to process logs.
|
||||
handler RecordHandler = TextHandler{
|
||||
@@ -239,11 +259,20 @@ pub fn new(config LogConfig) StructuredLog {
|
||||
|
||||
mut extra_fields := []Field{}
|
||||
|
||||
mut timestamp := logger.timestamp
|
||||
mut timestamp_old := Timestamp{
|
||||
format: logger.timestamp_format
|
||||
custom: logger.timestamp_custom
|
||||
local: logger.timestamp_local
|
||||
}
|
||||
if timestamp != timestamp_old {
|
||||
timestamp = timestamp_old
|
||||
}
|
||||
|
||||
if logger.add_timestamp {
|
||||
extra_fields << Field{
|
||||
name: 'timestamp'
|
||||
value: timestamp(logger.timestamp_format, logger.timestamp_custom,
|
||||
logger.timestamp_local)
|
||||
value: timestamp.as_value()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user