aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/log_spec.lua
blob: 0dc13e1a893b063e4b084f3ca586c6f7ce21a2ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local describe, it, before_each = t.describe, t.it, t.before_each
local assert_log = t.assert_log
local assert_nolog = t.assert_nolog
local clear = n.clear
local eq = t.eq
local exec_lua = n.exec_lua
local write_file = t.write_file

describe('vim.log', function()
  local xstate = 'Xstate-log'
  local caller_script ---@type string?

  ---@param name string
  ---@return string
  local function get_logfile(name)
    return exec_lua(function(logger_name)
      return vim.fs.normalize(vim.fs.joinpath(vim.fn.stdpath('log'), logger_name:lower() .. '.log'))
    end, name)
  end

  before_each(function()
    clear({ env = { XDG_STATE_HOME = xstate } })
    caller_script = nil
  end)

  it('new() creates a logger with the documented defaults', function()
    local info = exec_lua(function()
      local logger = vim.log.new({ name = 'MyPlugin' })
      local logfile = vim.fs.joinpath(vim.fn.stdpath('log'), 'myplugin.log')
      logger.info('skip')
      logger.warn('keep')
      return {
        level = vim.log.get_level(logger),
        writers = {
          type(logger.trace),
          type(logger.debug),
          type(logger.info),
          type(logger.warn),
          type(logger.error),
        },
        logfile = vim.fs.normalize(logfile),
        exists = vim.uv.fs_stat(logfile) ~= nil,
      }
    end)

    eq(3, info.level)
    eq({ 'function', 'function', 'function', 'function', 'function' }, info.writers)
    eq(get_logfile('MyPlugin'), info.logfile)
    eq(true, info.exists)

    local logfile = get_logfile('MyPlugin')
    assert_log('%[START%]%[.+%] MyPlugin logging initiated', logfile, 10)
    assert_log('%[WARN%].-\t"keep"', logfile, 10)
    assert_nolog('skip', logfile, 10)
  end)

  it('writer methods do nothing when called nil arguments', function()
    eq(
      { true, true, true, true, true, false },
      exec_lua(function()
        local logger = vim.log.new({ name = 'NoArgs', level = vim.log.levels.TRACE })
        local logfile = vim.fs.joinpath(vim.fn.stdpath('log'), 'noargs.log')
        return {
          logger.trace(),
          logger.debug(),
          logger.info(),
          logger.warn(),
          logger.error(),
          vim.uv.fs_stat(logfile) ~= nil,
        }
      end)
    )
  end)

  it('new() respects level and format_func opts', function()
    exec_lua(function()
      local logger = vim.log.new({
        name = 'CustomFormat',
        level = vim.log.levels.INFO,
        format_func = function(min_level, level, ...)
          if level < min_level then
            return nil
          end
          return tostring(select(1, ...)) .. '\n'
        end,
      })

      logger.trace('trace')
      logger.debug('debug')
      logger.info('info')
      logger.warn('warn')
      logger.error('error')
    end)

    local logfile = get_logfile('CustomFormat')
    assert_log('%[START%]%[.+%] CustomFormat logging initiated', logfile, 10)
    assert_nolog('trace', logfile, 10)
    assert_nolog('debug', logfile, 10)
    assert_log('info', logfile, 10)
    assert_log('warn', logfile, 10)
    assert_log('error', logfile, 10)
  end)

  it('set_level() changes filtering and get_level() reports the new level', function()
    local level = exec_lua(function()
      local logger = vim.log.new({ name = 'SetLevel' })

      vim.log.set_level(logger, vim.log.levels.INFO)
      logger.debug('skip')
      logger.info('keep')

      return vim.log.get_level(logger)
    end)

    eq(2, level)

    local logfile = get_logfile('SetLevel')
    assert_log('%[START%]%[.+%] SetLevel logging initiated', logfile, 10)
    assert_log('keep', logfile, 10)
    assert_nolog('skip', logfile, 10)
  end)

  it('set_format_func() replaces the formatter and can skip entries', function()
    exec_lua(function()
      local logger = vim.log.new({
        name = 'SetFormat',
        level = vim.log.levels.TRACE,
        format_func = function()
          return 'old\n'
        end,
      })

      vim.log.set_format_func(logger, function(min_level, level, ...)
        return table.concat({ 'new', min_level, level, tostring(select(1, ...)) }, '|') .. '\n'
      end)

      logger.error('formatted')

      vim.log.set_format_func(logger, function()
        return nil
      end)

      logger.error('skip-me')
    end)

    local logfile = get_logfile('SetFormat')
    assert_log('%[START%]%[.+%] SetFormat logging initiated', logfile, 10)
    assert_log('formatted', logfile, 10)
    assert_nolog('old', logfile, 10)
    assert_nolog('skip%-me', logfile, 10)
  end)

  it('default formatter logs the real caller source and line', function()
    caller_script = t.tmpname(false) .. '.lua'
    write_file(
      caller_script,
      "local logger = vim.log.new({ name = 'Caller', level = vim.log.levels.TRACE })\n"
        .. "logger.info('from-script')\n",
      true
    )

    exec_lua(function(path)
      vim.cmd('source ' .. vim.fn.fnameescape(path))
    end, caller_script)

    local logfile = get_logfile('Caller')
    local expected = exec_lua(function(path)
      return vim.pesc(vim.fn.fnamemodify(path, ':t'))
    end, caller_script)
    assert_log(expected .. ':2', logfile, 10)
    assert_log('from%-script', logfile, 10)
    assert_nolog('runtime/lua/vim/log%.lua', logfile, 10)
  end)
end)