luvit / lit

Toolkit for developing, sharing, and running luvit/lua programs and libraries.
http://lit.luvit.io/
Apache License 2.0
245 stars 58 forks source link

Add an ability to hook the log #90

Closed sousoux closed 9 years ago

sousoux commented 9 years ago

Lit currently picks up its log function from ./log which makes if difficult to redirect without a custom package. Would be good if you could pass a log function to core in the config table.

creationix commented 9 years ago

Good idea. A hacky way is to replace require('pretty-print').stdout, then require log, then replace stdout with the original value.

do
  local pp = require('pretty-print')
  local stdout = pp.stdout
  local out = {}
  function out:write(...)
    -- Custom output
  end
  pp.stdout = out
  -- This has to be the first ever require to log, so do this before loading lit.
  require('lit/libs/log')
  pp.stdout = stdout
end

It's hacky, I'll see about making an official API for it.

creationix commented 9 years ago

Ok, the interface to log changed a little. To set a custom stream for the logger to write to do:

local out = {}
function out:write(...)
  -- custom output
end
require('lit/libs/log').stream = out

This can happen at any time before or after the first require to log happens.

sousoux commented 9 years ago

Many thanks