All Projects → Tastyep → structlog.nvim

Tastyep / structlog.nvim

Licence: MIT license
Structured Logging for nvim, using Lua

Programming Languages

lua
6591 projects
shell
77523 projects
Makefile
30231 projects

Projects that are alternatives of or similar to structlog.nvim

onedark.nvim
One dark and light colorscheme for neovim >= 0.5.0 written in lua based on Atom's One Dark and Light theme. Additionally, it comes with 5 color variant styles
Stars: ✭ 425 (+1187.88%)
Mutual labels:  nvim
vim-filetype-formatter
Format program files in vim using your favorite command line formatter
Stars: ✭ 21 (-36.36%)
Mutual labels:  nvim
Telegraph.nvim
Send commands system commands in an elegant way
Stars: ✭ 33 (+0%)
Mutual labels:  nvim
fm-nvim
🗂 Neovim plugin that lets you use your favorite terminal file managers (and fuzzy finders) from within Neovim.
Stars: ✭ 114 (+245.45%)
Mutual labels:  nvim
LunarVim
An IDE layer for Neovim with sane defaults. Completely free and community driven.
Stars: ✭ 9,296 (+28069.7%)
Mutual labels:  nvim
dotfiles
This is what I use to get things done!
Stars: ✭ 59 (+78.79%)
Mutual labels:  nvim
org-bullets.nvim
No description or website provided.
Stars: ✭ 58 (+75.76%)
Mutual labels:  nvim
nvim
Personal neovim config
Stars: ✭ 24 (-27.27%)
Mutual labels:  nvim
dotfiles
My dotfiles for Neovim, Kitty terminal, Zsh, and a few other things.
Stars: ✭ 101 (+206.06%)
Mutual labels:  nvim
quick.nvim
A very fast Lua based Neovim configuration that uses coc.nvim for intellisense
Stars: ✭ 159 (+381.82%)
Mutual labels:  nvim
dotvim
My dotvim - work in progress - Beta stages
Stars: ✭ 27 (-18.18%)
Mutual labels:  nvim
dotfiles
⚫📁 Dotfiles
Stars: ✭ 23 (-30.3%)
Mutual labels:  nvim
fzf-lsp.nvim
Enable the power of fzf fuzzy search for the neovim built in lsp
Stars: ✭ 143 (+333.33%)
Mutual labels:  nvim
cosmos-logging
Logging component for .NET Core with nice APIs for developers to use.
Stars: ✭ 28 (-15.15%)
Mutual labels:  structured-logging
aquarium-vim
🌊 Aquarium, a simple vibrant dark theme for vim 🗒
Stars: ✭ 151 (+357.58%)
Mutual labels:  nvim
nvim-startup.lua
Displays neovim startup time
Stars: ✭ 53 (+60.61%)
Mutual labels:  nvim
timber-js
🌲 Great Node/JS logging made easy
Stars: ✭ 43 (+30.3%)
Mutual labels:  structured-logging
coc-flutter-tools
Rich Flutter development experience for (Neo)vim
Stars: ✭ 51 (+54.55%)
Mutual labels:  nvim
move.nvim
Gain the power to move lines and blocks and auto-indent them!
Stars: ✭ 109 (+230.3%)
Mutual labels:  nvim
echopraxia
Java Logging API with clean and simple structured logging and conditional & contextual features. JSON implementations in Logback and Log4J.
Stars: ✭ 37 (+12.12%)
Mutual labels:  structured-logging

structlog.nvim

Luarocks - structlog.nvim GitHub tag License

test sanitize Documentation

Structured Logging for nvim, using Lua

Why using it

structlog makes logging in Lua less painful and more powerful by adding structure to your log entries.

Instead of writting complex messages, you can start thinking in terms of an event that happens in the context of key/value pairs.
Each log entry is a meaningful dictionary instead of an opaque string!

Thanks to its flexible design, the structure of the final log output is up for you to decide.
Each log entry goes through a processor pipeline that is just a chain of functions that receive a dictionary and return a new dictionary that gets fed into the next function. That allows for simple but powerful data manipulation.
This dictionary is then formatted and sent out to the sink.

For more details, consider reading the documentation.

Installation

Using packer.nvim

use { "Tastyep/structlog.nvim" }

Using luarocks

luarocks install --local structlog.nvim

Design

As explained in the introduction, log messages go through a pipeline of functions to provide common information and to structure them into a comprehensible format. Internally, the log message is a dictionary built by the logger and is composed as follow:

  local kwargs = {
    level = Level.name(level), -- The log level represented as a string
    msg = msg,                 -- The given message
    events = events or {},     -- The dictionary containing the 'key=value' arguments
  }

At the end of the processing pipeline, the message msg field should contain the text to write to the sinks.

Processors

Processors are functions with the goal of enriching log messages. These functions accept one parameter, kwargs which they edit by adding new key=value pairs, such as the logger's name or the current timestamp, and return it on completion.

See the processors documentation.

Formatters

Formatters define the structure of the log. By default vim.inspect is used to format the given arguments events as key=value pairs. All formatters have the same interface. They expose a formatting function accepting a dictionary kwargs and return that same dictionary, modified so that kwargs.msg contains the log to write to the sink.

See the formatters documentation.

Sinks

Sinks specify where to write the log message. Like the other elements of the pipeline, sinks accept kwargs as parameter.

See the sunks documentation.

Usage

Create and Use

local log = require("structlog")

local logger = log.Logger("name", {
  log.sinks.Console(
    log.level.INFO
    {
      processors = {
        log.processors.Namer(),
        log.processors.Timestamper("%H:%M:%S"),
      },
      formatter = log.formatters.Format( --
        "%s [%s] %s: %-30s",
        { "timestamp", "level", "logger_name", "msg" }
      ),
    }
  ),
})

logger:info("A log message")
logger:warn("A log message with keyword arguments", { warning = "something happened" })
10:32:40 [INFO] name: A log message
10:32:40 [WARN] name: A log message with keyword arguments     warning="something happened"

Configure and Retrieve

local log = require("structlog")

log.configure({
  name = {
    sinks = {
      log.sinks.Console(
        log.level.INFO,
        {
          processors = {
            log.processors.Namer(),
            log.processors.Timestamper("%H:%M:%S"),
          },
          formatter = log.formatters.Format( --
            "%s [%s] %s: %-30s",
            { "timestamp", "level", "logger_name", "msg" },
          ),
        }
      ),
    },
  },
  other_logger = {...},
})

local logger = log.get_logger("name")

Example

local log = require("structlog")

log.configure({
  name = {
    sinks = {
      log.sinks.Console(
        log.level.INFO,
        {
          processors = {
            log.processors.Namer(),
            log.processors.StackWriter({ "line", "file" }, { max_parents = 0, stack_level = 0 }),
            log.processors.Timestamper("%H:%M:%S"),
          },
          formatter = log.formatters.FormatColorizer( --
            "%s [%s] %s: %-30s",
            { "timestamp", "level", "logger_name", "msg" },
            { level = log.formatters.FormatColorizer.color_level() }
          ),
        }
      ),
      log.sinks.NvimNotify(
        log.level.WARN,
        {
          processors = {
            log.processors.Namer(),
          },
          formatter = log.formatters.Format( --
            "%s",
            { "msg" },
            { blacklist = { "level", "logger_name" } }
          ),
          params_map = { title = "logger_name" },
        }),
      log.sinks.File(
        log.level.TRACE,
        "./test.log",
        {
          processors = {
            log.processors.Namer(),
            log.processors.StackWriter({ "line", "file" }, { max_parents = 3 }),
            log.processors.Timestamper("%H:%M:%S"),
          },
          formatter = log.formatters.Format( --
            "%s [%s] %s: %-30s",
            { "timestamp", "level", "logger_name", "msg" }
          ),
        }
      ),
    },
  },
  -- other_logger = {...}
})

local logger = log.get_logger("name")
logger:info("A log message")
logger:warn("A log message with keyword arguments", { warning = "something happened" })

image image

cat test.log:
10:43:23 [INFO] name: A log message                            file="lua/lsp/null-ls/formatters.lua", line=9
10:43:23 [WARN] name: A log message with keyword arguments     file="lua/lsp/null-ls/formatters.lua", line=10, warning="something happened"
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].