├── img ├── styles.jpg ├── custom-plugin.jpg ├── inspect-table.jpg └── combined-styles.jpg ├── config.ld ├── lualog ├── colors.lua ├── colorizer.lua ├── util.lua ├── inspect.lua └── init.lua ├── lualog-0.1-3.rockspec ├── LICENSE.md ├── example.lua ├── docs ├── classes │ ├── Colorizer.html │ ├── Inspect.html │ └── Lualog.html ├── index.html ├── examples │ └── example.lua.html ├── modules │ ├── init.html │ └── util.html ├── ldoc.css └── topics │ └── readme.md.html └── README.md /img/styles.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Desvelao/lualog/HEAD/img/styles.jpg -------------------------------------------------------------------------------- /img/custom-plugin.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Desvelao/lualog/HEAD/img/custom-plugin.jpg -------------------------------------------------------------------------------- /img/inspect-table.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Desvelao/lualog/HEAD/img/inspect-table.jpg -------------------------------------------------------------------------------- /img/combined-styles.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Desvelao/lualog/HEAD/img/combined-styles.jpg -------------------------------------------------------------------------------- /config.ld: -------------------------------------------------------------------------------- 1 | project = 'Lualog' 2 | description = 'Simple logger for Lua' 3 | full_description = [[ 4 | ]] 5 | title = 'Lualog - Simple logger for Lua' 6 | file = {'lualog',exclude = {}} 7 | dir = 'docs' 8 | format = 'markdown' 9 | readme = "README.md" 10 | examples = {'example.lua', exclude = {}} -------------------------------------------------------------------------------- /lualog/colors.lua: -------------------------------------------------------------------------------- 1 | return { 2 | reset = 0, 3 | black = 30, 4 | red = 31, 5 | green = 32, 6 | yellow = 33, 7 | blue = 34, 8 | magenta = 35, 9 | cyan = 36, 10 | white = 37, 11 | bgblack = 40, 12 | bgred = 41, 13 | bggreen = 42, 14 | bgyellow = 43, 15 | bgblue = 44, 16 | bgmagenta = 45, 17 | bgcyan = 46, 18 | bgwhite = 47, 19 | bold = 1, 20 | underlined = 4, 21 | reversed = 7 22 | } -------------------------------------------------------------------------------- /lualog-0.1-3.rockspec: -------------------------------------------------------------------------------- 1 | package = "lualog" 2 | version = "0.1-3" 3 | rockspec_format = "3.0" 4 | source = { 5 | url = "git://github.com/Desvelao/lualog", 6 | tag = "v0.1.3", 7 | dir = "lualog" 8 | } 9 | description = { 10 | summary = "Simple logger for Lua.", 11 | detailed = [[ 12 | Create a simple logger for Lua with some configurations as show date, stylized tags and logger name. 13 | ]], 14 | homepage = "https://github.com/Desvelao/lualog", 15 | issues_url = "https://github.com/Desvelao/lualog/issues", 16 | maintainer = "Desvelao", 17 | license = "MIT" 18 | } 19 | dependencies = { 20 | "lua >= 5.1" 21 | } 22 | build = { 23 | type = "builtin", 24 | modules = { 25 | ["lualog.init"] = "lualog/init.lua", 26 | ["lualog.colorizer"] = "lualog/colorizer.lua", 27 | ["lualog.colors"] = "lualog/colors.lua", 28 | ["lualog.inspect"] = "lualog/inspect.lua", 29 | ["lualog.util"] = "lualog/util.lua", 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Desvelao^^ 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /lualog/colorizer.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | #Colorizer class file 3 | ]] 4 | local colors = require'lualog.colors' 5 | --- Add colors to strings to print trough console/terminal 6 | -- @classmod Colorizer 7 | local Colorizer = {} 8 | Colorizer.__index = Colorizer 9 | Colorizer.__call = function(cls,...) 10 | return cls.new(...) 11 | end 12 | 13 | local ustring = function(value) 14 | return '\u{001b}['..value..'m' 15 | end 16 | 17 | --- Create a new Colorizer instance 18 | -- @param options Colorizer options 19 | -- @treturn Colorizer 20 | function Colorizer.new(options) 21 | -- options = options or {} 22 | local c = {} 23 | -- c.level = options.level or 1 24 | -- c.enabled = options.enabled or c.level > 0 25 | for color,value in pairs(colors) do 26 | if(color ~= 'reset') then 27 | c[color] = function (s) 28 | io.write(ustring(value)..s..ustring(colors.reset)) 29 | return c 30 | end 31 | 32 | end 33 | end 34 | c.ustr = function(style, str) 35 | local styles = {} 36 | for s in string.gmatch(style, "[^%.]+") do 37 | table.insert(styles, s) 38 | end 39 | local style = '' 40 | for _, sty in ipairs(styles) do 41 | if(colors[sty])then 42 | style = style..ustring(colors[sty]) 43 | end 44 | end 45 | return style..str..ustring(colors.reset) 46 | end 47 | c.codes = colors 48 | return setmetatable(c,Colorizer) 49 | end 50 | 51 | return Colorizer.new() -------------------------------------------------------------------------------- /example.lua: -------------------------------------------------------------------------------- 1 | -- Example 2 | local Lualog = require'lualog' 3 | 4 | -- Create a instance 5 | local logger = Lualog.new{ -- you can use Lualog() instead Lualog.lew 6 | tag = 'My Logger', -- a logger tag. default: '' 7 | styles = {dev = 'yellow', crash = 'bgred', data= 'red'}, -- define custom methods and their style. default: {} 8 | ignore_levels = {'dev'}, -- ignore levels (array). default: {} 9 | datestring = '%H:%M:%S', -- date string for os.date(). default: false => Not show 10 | table_inspect = { 11 | prettyfy = true, -- pretty print tables 12 | allow_tostring = true, -- allow table __tostring methamethod to apply instead plugin 13 | level_depth = 0 -- max table level to inspect 14 | }, 15 | plugins = {'table-inspect'} -- Add plugins to transform the elements you log. default = {} 16 | -- 'table-inspect' is a predefined plugin to inspect tables 17 | } 18 | 19 | -- Default logging methods 20 | logger.info('Hi. This\' a info message') 21 | logger.warn('This\' a warning message') 22 | logger.error('A fatal error just happened!') 23 | 24 | -- Custom defined logging methods 25 | logger.dev('Ignored level. See ignore_levels table field') -- this log is ignored due to ignore_levels = {'dev'} 26 | logger.crash('A crash!','Oh no!') -- support multiple args 27 | logger.data('My data').info('A log chainning method!') -- chain logging methods but they print at new lines 28 | 29 | -- Logging without tags 30 | logger('my_string', true, 2, {id = 'my-id'}) -- 31 | logger:print('my_string', true, 2, {id = 'my-id'}) -- same logger() 32 | 33 | -- Add custom plugins 34 | function my_plugin(element) 35 | if(type(element) == 'table' and element.value > 4)then 36 | return '' -- return a non-nil value to transform your element to logging. 37 | -- If return nil or no return, next plugin in the plugins queue is executed. 38 | end 39 | end 40 | 41 | logger:use(my_plugin) -- my_plugin is added to plugins queue and will be first to try to apply 42 | 43 | logger({value = 10, other_key = 'key-value'}) 44 | 45 | -- Log to console/terminal 46 | -- '' 47 | -------------------------------------------------------------------------------- /docs/classes/Colorizer.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Lualog - Simple logger for Lua 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 15 |
16 |
17 |
18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 59 | 60 |
61 | 62 |

Class Colorizer

63 |

Add colors to strings to print trough console/terminal

64 |

65 | 66 |

67 | 68 | 69 |

Methods

70 | 71 | 72 | 73 | 74 | 75 |
Colorizer:new (options)Create a new Colorizer instance
76 | 77 |
78 |
79 | 80 | 81 |

Methods

82 | 83 |
84 |
85 | 86 | Colorizer:new (options) 87 |
88 |
89 | Create a new Colorizer instance 90 | 91 | 92 |

Parameters:

93 |
    94 |
  • options 95 | Colorizer options 96 |
  • 97 |
98 | 99 |

Returns:

100 |
    101 | 102 | Colorizer 103 | 104 | 105 | 106 |
107 | 108 | 109 | 110 | 111 |
112 |
113 | 114 | 115 |
116 |
117 |
118 | generated by LDoc 1.4.6 119 | Last updated 2019-05-06 01:56:35 120 |
121 |
122 | 123 | 124 | -------------------------------------------------------------------------------- /lualog/util.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | #Utils 3 | ]] 4 | local util = { 5 | table = {}, 6 | deco = {}, 7 | value = {} 8 | } 9 | 10 | --- Returns whether a table includes a certain element. 11 | -- @tparam table t The table that may include the element 12 | -- @param element The element to look for 13 | -- @treturn boolean Whether t includes element 14 | function util.table.includes(t,element) 15 | local result = nil 16 | for _,v in pairs(t) do 17 | if(v == element)then result = v; break end 18 | end 19 | return result 20 | end 21 | 22 | --- Returns whether or not its argument is a table 23 | -- @param t Any value 24 | -- @treturn boolean Whether t is a table 25 | function util.table.is(t) 26 | return type(t) == 'table' 27 | end 28 | 29 | --- Applies a function to every element of a table and collects the results in a new table. 30 | -- Arguments passed to the function are Value, Key and the Table. 31 | -- @tparam table t A table of values 32 | -- @tparam function fn A function to apply to every value 33 | -- @treturn table The return values of each call to fn 34 | function util.table.map(t,fn) 35 | local new_t = {} 36 | for k,v in pairs(t) do 37 | new_t[k] = fn(v,k,t) 38 | end 39 | return new_t 40 | end 41 | 42 | --- Applies a function to each element of a tabla and throws away the results. 43 | -- Arguments passed to the function are Value, Key and the Table. 44 | -- @tparam table t A table of values 45 | -- @tparam function fn A function to apply to every value 46 | function util.table.for_each(t,fn) 47 | for k,v in pairs(t) do 48 | fn(v,k,t) 49 | end 50 | end 51 | 52 | --- Returns whether an object is an instance of a certain class. 53 | -- @tparam table instance The object to check 54 | -- @tparam string class The class name the object might be an instance of 55 | -- @treturn boolean Whether the object is an instance of the class 56 | function util.is_instance_of(instance,class) 57 | if not type(instance)=='table' then error("Argument #1 not an object (expected table, got "..type(instance)..")", 2) end 58 | return instance.__class_name == class 59 | end 60 | 61 | do 62 | local function return_first(first) return first end 63 | local function return_second(first, second) return second end 64 | 65 | --- Given a condition, returns a function that returns 66 | -- either its first argument (if condition is true) 67 | -- or its second argument (if condition is false) 68 | -- @param condition Any Lua value 69 | -- @treturn function One of the two functions described above 70 | function util.deco.if_condition(condition) 71 | if condition then 72 | return return_first 73 | else 74 | return return_second 75 | end 76 | end 77 | end 78 | 79 | --- Checks whether a value is nil and returns a default value if so. 80 | -- @param value Value to check if is nil 81 | -- @param default Value to return if value param is nil 82 | -- @return default if value is nil, value otherwise 83 | function util.value.not_nil(value,default) 84 | return (value == nil and default) or value 85 | end 86 | 87 | return util 88 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Lualog - Simple logger for Lua 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 15 |
16 |
17 |
18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 52 | 53 |
54 | 55 | 56 |

Simple logger for Lua

57 |

58 | 59 |

60 | 61 |

Modules

62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 |
initCreate a Lualog instance
utilReturns whether a table includes a certain element.
72 |

Classes

73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 |
ColorizerAdd colors to strings to print trough console/terminal
InspectAn object that inspects a Lua table recursively and generates human-readable output.
83 |

Topics

84 | 85 | 86 | 87 | 88 | 89 |
readme.md
90 |

Examples

91 | 92 | 93 | 94 | 95 | 96 |
example.lua
97 | 98 |
99 |
100 |
101 | generated by LDoc 1.4.6 102 | Last updated 2019-05-06 01:56:35 103 |
104 |
105 | 106 | 107 | -------------------------------------------------------------------------------- /lualog/inspect.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | #Inspect class file 3 | ]] 4 | 5 | local Colorizer = require'lualog.colorizer' 6 | local util = require'lualog.util' 7 | 8 | --- An object that inspects a Lua table recursively and generates human-readable output. 9 | -- @classmod Inspect 10 | 11 | local Inspect = {} 12 | 13 | --- Default Options 14 | Inspect.config = { 15 | prettyfy = false, -- Prettify Output 16 | allow_tostring = true, -- Tries calling the __tostring metamethod 17 | level_depth = 0, -- Max depth to recurse into tables 18 | type_values = { -- Variables types color configuration 19 | number = {style = 'green'}, 20 | string = {style = 'yellow', render = function(value) return '"'..value..'"' end}, 21 | ["function"] = {style = 'blue'}, 22 | table = {style = 'red'}, 23 | boolean = {style = 'magenta'}, 24 | ["nil"] = {style = 'cyan'} 25 | } 26 | } 27 | 28 | --- Creates a new Inspector object. 29 | -- @tparam table options A table containing options for the new inspector 30 | -- @treturn Inspector A newly created inspector 31 | function Inspect.new(options) 32 | local config = options or {} 33 | local inspect = {config = setmetatable(options or {}, {__index = Inspect.config})} 34 | return setmetatable(inspect, {__index = Inspect}) 35 | end 36 | 37 | --- Inspects an object recursively 38 | -- @tparam table o An object to inspect 39 | -- @tparam table options Additional one-time options for this call 40 | -- @treturn table parsed results of the inspection 41 | function Inspect:parse(o, options) 42 | options = options or {} 43 | local config = { 44 | prettyfy = options.prettyfy or self.config.prettyfy, 45 | allow_tostring = options.allow_tostring or self.config.allow_tostring, 46 | level_depth = options.level_depth or self.config.level_depth, 47 | _level = options._level or 1 48 | } 49 | local if_prettyfy = util.deco.if_condition(config.prettyfy) 50 | local char_pretiffy = ' ' 51 | local result = '{' .. if_prettyfy('\n','') 52 | local length = 0 53 | for k,v in pairs(o) do 54 | length = length + 1 55 | if(config.allow_tostring) then 56 | return tostring(v) 57 | end 58 | local val, style_elem, render_elem = v, '', function(value) return value end 59 | result = result .. if_prettyfy(char_pretiffy:rep(config._level),'') .. k .. ' = ' 60 | if(type(v) == 'table' and (config.level_depth == 0 or config.level_depth > config._level)) then 61 | val = self:parse(v, {level_depth = config.level_depth, _level = config._level + 1, prettyfy = config.prettyfy, allow_tostring = config.allow_tostring}) 62 | else 63 | val = v 64 | style_elem = (self.config.type_values[type(v)] and self.config.type_values[type(v)].style) or '' 65 | render_elem = (self.config.type_values[type(v)] and self.config.type_values[type(v)].render) or render_elem 66 | end 67 | result = result .. Colorizer.ustr(style_elem,tostring(render_elem(val)))..', ' .. if_prettyfy('\n','') 68 | end 69 | result = result:sub(0,if_prettyfy(-4,-3)) .. (length > 0 and if_prettyfy('\n' .. char_pretiffy:rep(config._level-1),'') or '') .. (length > 0 and '}' or '{}') 70 | local parsed = { text = result} 71 | 72 | return setmetatable(parsed,{ 73 | __index = { 74 | print = function(t) print(t.text) end 75 | }, 76 | __tostring = function(t) return t.text end} 77 | ) 78 | end 79 | 80 | return Inspect 81 | -------------------------------------------------------------------------------- /docs/classes/Inspect.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Lualog - Simple logger for Lua 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 15 |
16 |
17 |
18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 60 | 61 |
62 | 63 |

Class Inspect

64 |

An object that inspects a Lua table recursively and generates human-readable output.

65 |

66 | 67 |

68 | 69 | 70 |

Tables

71 | 72 | 73 | 74 | 75 | 76 |
Inspect.configDefault Options
77 |

Methods

78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 |
Inspect:new (options)Creates a new Inspector object.
Inspect:parse (o, options)Inspects an object recursively
88 | 89 |
90 |
91 | 92 | 93 |

Tables

94 | 95 |
96 |
97 | 98 | Inspect.config 99 |
100 |
101 | Default Options 102 | 103 | 104 |

Fields:

105 |
    106 |
  • prettyfy 107 | Prettify Output 108 |
  • 109 |
  • allow_tostring 110 | Tries calling the __tostring metamethod 111 |
  • 112 |
  • level_depth 113 | Max depth to recurse into tables 114 |
  • 115 |
  • type_values 116 | Variables types color configuration 117 |
  • 118 |
119 | 120 | 121 | 122 | 123 | 124 |
125 |
126 |

Methods

127 | 128 |
129 |
130 | 131 | Inspect:new (options) 132 |
133 |
134 | Creates a new Inspector object. 135 | 136 | 137 |

Parameters:

138 |
    139 |
  • options 140 | table 141 | A table containing options for the new inspector 142 |
  • 143 |
144 | 145 |

Returns:

146 |
    147 | 148 | Inspector 149 | A newly created inspector 150 |
151 | 152 | 153 | 154 | 155 |
156 |
157 | 158 | Inspect:parse (o, options) 159 |
160 |
161 | Inspects an object recursively 162 | 163 | 164 |

Parameters:

165 |
    166 |
  • o 167 | table 168 | An object to inspect 169 |
  • 170 |
  • options 171 | table 172 | Additional one-time options for this call 173 |
  • 174 |
175 | 176 |

Returns:

177 |
    178 | 179 | table 180 | parsed results of the inspection 181 |
182 | 183 | 184 | 185 | 186 |
187 |
188 | 189 | 190 |
191 |
192 |
193 | generated by LDoc 1.4.6 194 | Last updated 2019-05-06 01:56:35 195 |
196 |
197 | 198 | 199 | -------------------------------------------------------------------------------- /docs/examples/example.lua.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Lualog - Simple logger for Lua 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 15 |
16 |
17 |
18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 55 | 56 |
57 | 58 |

example.lua

59 |
 60 | -- Example
 61 | local Lualog = require'lualog'
 62 | 
 63 | -- Create a instance
 64 | local logger = Lualog.new{ -- you can use Lualog() instead Lualog.lew
 65 |     tag = 'My Logger', -- a logger tag. default: ''
 66 |     styles = {dev = 'yellow', crash = 'bgred', data= 'red'}, -- define custom methods and their style. default: {}
 67 |     ignore_levels = {'dev'}, -- ignore levels (array). default: {}
 68 |     datestring = '%H:%M:%S', -- date string for os.date(). default: false => Not show
 69 |      table_inspect = {
 70 |         prettyfy = true, -- pretty print tables
 71 |         allow_tostring = true, -- allow table __tostring methamethod to apply instead plugin
 72 |         level_depth = 0 -- max table level to inspect
 73 |     },
 74 |     plugins = {'table-inspect'} -- Add plugins to transform the elements you log. default = {}
 75 |                                 -- 'table-inspect' is a predefined plugin to inspect tables
 76 | }
 77 | 
 78 | -- Default logging methods
 79 | logger.info('Hi. This\' a info message')
 80 | logger.warn('This\' a warning message')
 81 | logger.error('A fatal error just happened!')
 82 | 
 83 | -- Custom defined logging methods
 84 | logger.dev('Ignored level. See ignore_levels table field') -- this log is ignored due to ignore_levels = {'dev'}
 85 | logger.crash('A crash!','Oh no!') -- support multiple args
 86 | logger.data('My data').info('A log chainning method!') -- chain logging methods but they print at new lines
 87 | 
 88 | -- Logging without tags
 89 | logger('my_string', true, 2, {id = 'my-id'}) --
 90 | logger:print('my_string', true, 2, {id = 'my-id'}) -- same logger()
 91 | 
 92 | -- Add custom plugins
 93 | function my_plugin(element)
 94 |     if(type(element) == 'table' and element.value > 4)then
 95 |         return '<Element value=' .. element.value .. '>' -- return a non-nil value to transform your element to logging.
 96 |         -- If return nil or no return, next plugin in the plugins queue is executed.
 97 |     end
 98 | end
 99 | 
100 | logger:use(my_plugin) -- my_plugin is added to plugins queue and will be first to try to apply
101 | 
102 | logger({value = 10, other_key = 'key-value'})
103 | 
104 | -- Log to console/terminal
105 | -- '<Element value=10>'
106 | 107 | 108 |
109 |
110 |
111 | generated by LDoc 1.4.6 112 | Last updated 2019-05-06 01:56:35 113 |
114 |
115 | 116 | 117 | -------------------------------------------------------------------------------- /docs/modules/init.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Lualog - Simple logger for Lua 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 15 |
16 |
17 |
18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 59 | 60 |
61 | 62 |

Module init

63 |

Create a Lualog instance

64 |

65 | 66 |

67 | 68 | 69 |

Functions

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 |
__call (t, ...)Print elements with plugins applied
info (...)Log info
warn (...)Log warn
error (...)Log error
Lualog:print (...)Function as print global function but apply your plugins for each element
Lualog:use (...)Add plugin/s to your lualog instance
Lualog:paint (style, text)Return a text colorized with style.
100 | 101 |
102 |
103 | 104 | 105 |

Functions

106 | 107 |
108 |
109 | 110 | __call (t, ...) 111 |
112 |
113 | Print elements with plugins applied 114 | 115 | 116 |

Parameters:

117 |
    118 |
  • t 119 | Lualog 120 | 121 | 122 | 123 |
  • 124 |
  • ... 125 | Elements to print 126 |
  • 127 |
128 | 129 | 130 | 131 | 132 | 133 |
134 |
135 | 136 | info (...) 137 |
138 |
139 | Log info 140 | 141 | 142 |

Parameters:

143 |
    144 |
  • ... 145 | Elements to print 146 |
  • 147 |
148 | 149 |

Returns:

150 |
    151 | 152 | self 153 |
154 | 155 | 156 | 157 | 158 |
159 |
160 | 161 | warn (...) 162 |
163 |
164 | Log warn 165 | 166 | 167 |

Parameters:

168 |
    169 |
  • ... 170 | Elements to print 171 |
  • 172 |
173 | 174 |

Returns:

175 |
    176 | 177 | self 178 |
179 | 180 | 181 | 182 | 183 |
184 |
185 | 186 | error (...) 187 |
188 |
189 | Log error 190 | 191 | 192 |

Parameters:

193 |
    194 |
  • ... 195 | Elements to print 196 |
  • 197 |
198 | 199 |

Returns:

200 |
    201 | 202 | self 203 |
204 | 205 | 206 | 207 | 208 |
209 |
210 | 211 | Lualog:print (...) 212 |
213 |
214 | Function as print global function but apply your plugins for each element 215 | 216 | 217 |

Parameters:

218 |
    219 |
  • ... 220 | Element to print 221 |
  • 222 |
223 | 224 | 225 | 226 | 227 | 228 |
229 |
230 | 231 | Lualog:use (...) 232 |
233 |
234 | Add plugin/s to your lualog instance 235 | 236 | 237 |

Parameters:

238 |
    239 |
  • ... 240 | function 241 | Plugin/s to add to the queue 242 |
  • 243 |
244 | 245 |

Returns:

246 |
    247 | 248 | self 249 |
250 | 251 | 252 | 253 | 254 |
255 |
256 | 257 | Lualog:paint (style, text) 258 |
259 |
260 | Return a text colorized with style. It's necesary use print function to see at console/terminal 261 | 262 | 263 |

Parameters:

264 |
    265 |
  • style 266 | string 267 | Style to apply to the text 268 |
  • 269 |
  • text 270 | string 271 | Text 272 |
  • 273 |
274 | 275 |

Returns:

276 |
    277 | 278 | string 279 | 280 | 281 | 282 |
283 | 284 | 285 | 286 | 287 |
288 |
289 | 290 | 291 |
292 |
293 |
294 | generated by LDoc 1.4.6 295 | Last updated 2019-05-06 01:56:35 296 |
297 |
298 | 299 | 300 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lualog 2 | This is a simple logger for Lua. 3 | 4 | Create a simple logger for Lua with some configurations as show date, stylized tags and logger name. 5 | 6 | **Not works with Luvit** (due to Luvit custom `require` function). 7 | 8 | # Docs 9 | 10 | See the documentation [here](https://desvelao.github.io/lualog/). 11 | 12 | # Install 13 | 14 | ## Using Luarocks 15 | ```bash 16 | luarocks install lualog #global installation 17 | luarocks install lualog --local # local installation 18 | luarocks install lualog --tree lua_modules # current directory installation to lua_modules folder 19 | ``` 20 | ## Manual 21 | 22 | Download this repository and copy the folder **lualog** and place it at **root folder of your proyect**. 23 | 24 | # Usage 25 | ```lua 26 | -- Example 27 | local Lualog = require'lualog' 28 | 29 | local logger = Lualog.new{ -- you can use Lualog() instead Lualog.new 30 | tag = 'My logger', -- a logger tag. default: '' 31 | styles = {dev = 'yellow', crash = 'bgred'}, -- define custom methods and their style. default: {} 32 | ignore_levels = {'dev'}, -- ignore levels (array). default: {} 33 | datestring = '%H:%M:%S', -- date string for os.date(). default: false => Not show 34 | table_inspect = { -- table-inspect plugin config 35 | prettyfy = true, -- pretty print tables. default: false 36 | allow_tostring = true, -- allow use table __tostring metamethod. default: true 37 | level_depth = 0 -- max nested level to inspect. default: 0. 0 means no level limit 38 | }, 39 | plugins = {'table-inspect'} -- add plugins to log items. table-inspect is a optional predefined plugin. 40 | -- You can add function plugins here too. See as define a plugin at Plugin section. default: {} => no plugins added. 41 | } 42 | -- Default logging methods 43 | logger.info('Hi. This\' a info message') 44 | logger.warn('This\' a warning message') 45 | logger.error('A fatal error just happened!') 46 | 47 | -- Custom methods defined at initiation instance 48 | logger.dev('Ignored level. See ignore_levels table field') -- This log is ignored due to ignore_levels = {'dev'} 49 | logger.crash('A crash!','Oh no!') -- Support multiple args 50 | logger.error('My error').info('A log chainning method!') -- chain logging methods but they print at new lines 51 | ``` 52 | 53 | Result: 54 | 55 | ![Styles](./img/styles.jpg) 56 | 57 | # Styles: customize the tag of your custom logging methods 58 | 59 | If you want to create your custom logging methods, you can add a custom style: 60 | 61 | - Color text: `black, red, green, yellow, blue, magenta, cyan, white` 62 | - Background color: `bgblack, bgred, bggreen, bgyellow, bgblue, bgmagenta, bgcyan, bgwhite` 63 | - Others styles: `bold, underlined, reversed` 64 | 65 | Add custom logging methods in options table when you init your instance with: 66 | 67 | ```lua 68 | local Lualog = require'lualog' -- equal to require('lualog') 69 | local logger = Lualog.new({ 70 | styles = { 71 | dev='yellow', -- tag = style 72 | my_log= 'red.bgblue' -- you can combine styles, red color and blue background for this method. 73 | } 74 | }) 75 | 76 | -- and using them 77 | logger.dev('Dev log') 78 | logger.my_log('Other custom logging method') 79 | ``` 80 | Result: 81 | 82 | ![Combining styles](./img/combined-styles.jpg) 83 | 84 | # Methods of instance 85 | 86 | When you create a Lualog instance, this has these methods (by default): 87 | 88 | ## Default logging methods 89 | 90 | - `.info(...)` - log method with style `blue` and tag `info` 91 | - `.warn(...)` - log method with style `yellow` and tag `warn` 92 | - `.error(...)` - log method with style `red` and tag `error` 93 | 94 | 95 | ## Advanced methods 96 | 97 | - `:print(...)`: function as `print` global function but **apply your plugins**. Or call your Lualog instance, logger in examples, as a function `logger(...)`. 98 | 99 | *Tip: call your Lualog instance as function instead `:print()`* 100 | ```lua 101 | logger:print(a_number, a_boolean, a_string, a_table) 102 | -- or better, you can only call your logger as function! It's the same ;) 103 | logger(a_number, a_boolean, a_string, a_table) -- you got a custom print function with custom logging plugins! 104 | ``` 105 | - `:use(...)`: add plugins to queue. See Plugin section. 106 | 107 | - `:paint(style, text)`: return a colorized string. Use a print method to see it at console/terminal. 108 | ```lua 109 | local my_blue_string = logger:paint('blue','This string will be blue if is printed') 110 | print(my_blue_string) 111 | ``` 112 | 113 | - `:inspect(table)`: inspect a table with **table_inspect** configuration defined at initiation the instance or default if not. 114 | ```lua 115 | local my_table = { 116 | str = 'string value', 117 | n = 1, 118 | bool = true, 119 | table = { 120 | nested_table = { 121 | my_nested_key = function() return 0 end 122 | } 123 | } 124 | } 125 | 126 | logger:inspect(my_table) -- inspect a table with yor table_inspect config defined or use default values. No plugin is applied. 127 | ``` 128 | Log: 129 | 130 | ![Inspect table](./img/inspect-table.jpg) 131 | 132 | # Plugin 133 | 134 | You can customize as an element is logged through to plugins. They are applied to logging methods and `:print` function. 135 | 136 | ```lua 137 | -- ... lualog instance defined 138 | function my_plugin_for_print_tables(element) 139 | if(type(element) == 'table') then 140 | -- if element is not a table, this plugin is ignored 141 | -- and pass to next plugin in queue because didn't return a not-nil value 142 | local result = '' 143 | for key, value in pairs(element) do 144 | result = result .. key .. ' = ' .. tostring(value) .. '\n' 145 | end 146 | return result -- return a not-nil value to print your element as you defined an this function/plugin 147 | end 148 | end 149 | 150 | logger:use(my_plugin_for_print_tables) 151 | 152 | logger:print({ 153 | key1 = 'value1', 154 | key2 = 'value2' 155 | }) 156 | 157 | -- Will print due to my_plugin_for_print_tables 158 | -- key1 = value1 159 | -- key2 = value2 160 | 161 | -- or define a plugin with an anonymous function 162 | logger:use(function(element) 163 | -- your plugin code 164 | end) 165 | 166 | -- :use(...) accept multiple plugins definitions too 167 | logger:use(my_plugin,other_plugin,...) 168 | ``` 169 | Log: 170 | 171 | ![Custom Plugin](./img/custom-plugin.jpg) 172 | 173 | You can add so many plugins as you want. Last plugins added are checked first in the queue. 174 | 175 | *Note: you can use plugins to define how to print your custom objects what are instance of a custom class.* 176 | 177 | **Order what your plugin is added is important. Last plugins added are checked first. If a plugin return a not-nil value, then that return value is logged. Of this way you can customize your logs.** 178 | 179 | 180 | # Issues requiring the module 181 | 182 | Be sure that module is accessible to path of **LUA_PATH** enviorement variable. 183 | 184 | *Note: Your **LUA_PATH** should contain `./?.lua;./?/init.lua;`* if you place lualog folder in your project root folder. 185 | 186 | _Note_: You can check your **LUA_PATH** adding to at beginning of your root script: 187 | 188 | ```lua 189 | -- Print your your LUA_PATH that Lua uses to require modules. 190 | print(package.path) 191 | ``` 192 | 193 | You can extends your **LUA_PATH** at beginning to your file when you init it or add the path where lualog is to your **LUA_PATH** enviorement variable. 194 | 195 | ```lua 196 | -- Extends your LUA_PATH when you init your script/program 197 | package.path = package.path .. './?.lua;./?/init.lua;' .. 'path/to/your/modules/?.lua' 198 | ``` 199 | 200 | # License 201 | MIT 202 | -------------------------------------------------------------------------------- /docs/ldoc.css: -------------------------------------------------------------------------------- 1 | /* BEGIN RESET 2 | 3 | Copyright (c) 2010, Yahoo! Inc. All rights reserved. 4 | Code licensed under the BSD License: 5 | http://developer.yahoo.com/yui/license.html 6 | version: 2.8.2r1 7 | */ 8 | html { 9 | color: #000; 10 | background: #FFF; 11 | } 12 | body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,blockquote,th,td { 13 | margin: 0; 14 | padding: 0; 15 | } 16 | table { 17 | border-collapse: collapse; 18 | border-spacing: 0; 19 | } 20 | fieldset,img { 21 | border: 0; 22 | } 23 | address,caption,cite,code,dfn,em,strong,th,var,optgroup { 24 | font-style: inherit; 25 | font-weight: inherit; 26 | } 27 | del,ins { 28 | text-decoration: none; 29 | } 30 | li { 31 | margin-left: 20px; 32 | } 33 | caption,th { 34 | text-align: left; 35 | } 36 | h1,h2,h3,h4,h5,h6 { 37 | font-size: 100%; 38 | font-weight: bold; 39 | } 40 | q:before,q:after { 41 | content: ''; 42 | } 43 | abbr,acronym { 44 | border: 0; 45 | font-variant: normal; 46 | } 47 | sup { 48 | vertical-align: baseline; 49 | } 50 | sub { 51 | vertical-align: baseline; 52 | } 53 | legend { 54 | color: #000; 55 | } 56 | input,button,textarea,select,optgroup,option { 57 | font-family: inherit; 58 | font-size: inherit; 59 | font-style: inherit; 60 | font-weight: inherit; 61 | } 62 | input,button,textarea,select {*font-size:100%; 63 | } 64 | /* END RESET */ 65 | 66 | body { 67 | margin-left: 1em; 68 | margin-right: 1em; 69 | font-family: arial, helvetica, geneva, sans-serif; 70 | background-color: #ffffff; margin: 0px; 71 | } 72 | 73 | code, tt { font-family: monospace; font-size: 1.1em; } 74 | span.parameter { font-family:monospace; } 75 | span.parameter:after { content:":"; } 76 | span.types:before { content:"("; } 77 | span.types:after { content:")"; } 78 | .type { font-weight: bold; font-style:italic } 79 | 80 | body, p, td, th { font-size: .95em; line-height: 1.2em;} 81 | 82 | p, ul { margin: 10px 0 0 0px;} 83 | 84 | strong { font-weight: bold;} 85 | 86 | em { font-style: italic;} 87 | 88 | h1 { 89 | font-size: 1.5em; 90 | margin: 20px 0 20px 0; 91 | } 92 | h2, h3, h4 { margin: 15px 0 10px 0; } 93 | h2 { font-size: 1.25em; } 94 | h3 { font-size: 1.15em; } 95 | h4 { font-size: 1.06em; } 96 | 97 | a:link { font-weight: bold; color: #004080; text-decoration: none; } 98 | a:visited { font-weight: bold; color: #006699; text-decoration: none; } 99 | a:link:hover { text-decoration: underline; } 100 | 101 | hr { 102 | color:#cccccc; 103 | background: #00007f; 104 | height: 1px; 105 | } 106 | 107 | blockquote { margin-left: 3em; } 108 | 109 | ul { list-style-type: disc; } 110 | 111 | p.name { 112 | font-family: "Andale Mono", monospace; 113 | padding-top: 1em; 114 | } 115 | 116 | pre { 117 | background-color: rgb(245, 245, 245); 118 | border: 1px solid #C0C0C0; /* silver */ 119 | padding: 10px; 120 | margin: 10px 0 10px 0; 121 | overflow: auto; 122 | font-family: "Andale Mono", monospace; 123 | } 124 | 125 | pre.example { 126 | font-size: .85em; 127 | } 128 | 129 | table.index { border: 1px #00007f; } 130 | table.index td { text-align: left; vertical-align: top; } 131 | 132 | #container { 133 | margin-left: 1em; 134 | margin-right: 1em; 135 | background-color: #f0f0f0; 136 | } 137 | 138 | #product { 139 | text-align: center; 140 | border-bottom: 1px solid #cccccc; 141 | background-color: #ffffff; 142 | } 143 | 144 | #product big { 145 | font-size: 2em; 146 | } 147 | 148 | #main { 149 | background-color: #f0f0f0; 150 | border-left: 2px solid #cccccc; 151 | } 152 | 153 | #navigation { 154 | float: left; 155 | width: 14em; 156 | vertical-align: top; 157 | background-color: #f0f0f0; 158 | overflow: visible; 159 | } 160 | 161 | #navigation h2 { 162 | background-color:#e7e7e7; 163 | font-size:1.1em; 164 | color:#000000; 165 | text-align: left; 166 | padding:0.2em; 167 | border-top:1px solid #dddddd; 168 | border-bottom:1px solid #dddddd; 169 | } 170 | 171 | #navigation ul 172 | { 173 | font-size:1em; 174 | list-style-type: none; 175 | margin: 1px 1px 10px 1px; 176 | } 177 | 178 | #navigation li { 179 | text-indent: -1em; 180 | display: block; 181 | margin: 3px 0px 0px 22px; 182 | } 183 | 184 | #navigation li li a { 185 | margin: 0px 3px 0px -1em; 186 | } 187 | 188 | #content { 189 | margin-left: 14em; 190 | padding: 1em; 191 | width: 700px; 192 | border-left: 2px solid #cccccc; 193 | border-right: 2px solid #cccccc; 194 | background-color: #ffffff; 195 | } 196 | 197 | #about { 198 | clear: both; 199 | padding: 5px; 200 | border-top: 2px solid #cccccc; 201 | background-color: #ffffff; 202 | } 203 | 204 | @media print { 205 | body { 206 | font: 12pt "Times New Roman", "TimeNR", Times, serif; 207 | } 208 | a { font-weight: bold; color: #004080; text-decoration: underline; } 209 | 210 | #main { 211 | background-color: #ffffff; 212 | border-left: 0px; 213 | } 214 | 215 | #container { 216 | margin-left: 2%; 217 | margin-right: 2%; 218 | background-color: #ffffff; 219 | } 220 | 221 | #content { 222 | padding: 1em; 223 | background-color: #ffffff; 224 | } 225 | 226 | #navigation { 227 | display: none; 228 | } 229 | pre.example { 230 | font-family: "Andale Mono", monospace; 231 | font-size: 10pt; 232 | page-break-inside: avoid; 233 | } 234 | } 235 | 236 | table.module_list { 237 | border-width: 1px; 238 | border-style: solid; 239 | border-color: #cccccc; 240 | border-collapse: collapse; 241 | } 242 | table.module_list td { 243 | border-width: 1px; 244 | padding: 3px; 245 | border-style: solid; 246 | border-color: #cccccc; 247 | } 248 | table.module_list td.name { background-color: #f0f0f0; min-width: 200px; } 249 | table.module_list td.summary { width: 100%; } 250 | 251 | 252 | table.function_list { 253 | border-width: 1px; 254 | border-style: solid; 255 | border-color: #cccccc; 256 | border-collapse: collapse; 257 | } 258 | table.function_list td { 259 | border-width: 1px; 260 | padding: 3px; 261 | border-style: solid; 262 | border-color: #cccccc; 263 | } 264 | table.function_list td.name { background-color: #f0f0f0; min-width: 200px; } 265 | table.function_list td.summary { width: 100%; } 266 | 267 | ul.nowrap { 268 | overflow:auto; 269 | white-space:nowrap; 270 | } 271 | 272 | dl.table dt, dl.function dt {border-top: 1px solid #ccc; padding-top: 1em;} 273 | dl.table dd, dl.function dd {padding-bottom: 1em; margin: 10px 0 0 20px;} 274 | dl.table h3, dl.function h3 {font-size: .95em;} 275 | 276 | /* stop sublists from having initial vertical space */ 277 | ul ul { margin-top: 0px; } 278 | ol ul { margin-top: 0px; } 279 | ol ol { margin-top: 0px; } 280 | ul ol { margin-top: 0px; } 281 | 282 | /* make the target distinct; helps when we're navigating to a function */ 283 | a:target + * { 284 | background-color: #FF9; 285 | } 286 | 287 | 288 | /* styles for prettification of source */ 289 | pre .comment { color: #558817; } 290 | pre .constant { color: #a8660d; } 291 | pre .escape { color: #844631; } 292 | pre .keyword { color: #aa5050; font-weight: bold; } 293 | pre .library { color: #0e7c6b; } 294 | pre .marker { color: #512b1e; background: #fedc56; font-weight: bold; } 295 | pre .string { color: #8080ff; } 296 | pre .number { color: #f8660d; } 297 | pre .operator { color: #2239a8; font-weight: bold; } 298 | pre .preprocessor, pre .prepro { color: #a33243; } 299 | pre .global { color: #800080; } 300 | pre .user-keyword { color: #800080; } 301 | pre .prompt { color: #558817; } 302 | pre .url { color: #272fc2; text-decoration: underline; } 303 | 304 | -------------------------------------------------------------------------------- /docs/modules/util.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Lualog - Simple logger for Lua 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 15 |
16 |
17 |
18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 59 | 60 |
61 | 62 |

Module util

63 |

Returns whether a table includes a certain element.

64 |

65 | 66 |

67 | 68 | 69 |

Functions

70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 92 | 93 | 94 | 95 | 96 | 97 |
util.table.is (t)Returns whether or not its argument is a table
util.table.map (t, fn)Applies a function to every element of a table and collects the results in a new table.
util.table.for_each (t, fn)Applies a function to each element of a tabla and throws away the results.
is_instance_of (instance, class)Returns whether an object is an instance of a certain class.
util.deco.if_condition (condition)Given a condition, returns a function that returns 90 | either its first argument (if condition is true) 91 | or its second argument (if condition is false)
util.value.not_nil (value, default)Checks whether a value is nil and returns a default value if so.
98 | 99 |
100 |
101 | 102 | 103 |

Functions

104 | 105 |
106 |
107 | 108 | util.table.is (t) 109 |
110 |
111 | Returns whether or not its argument is a table 112 | 113 | 114 |

Parameters:

115 |
    116 |
  • t 117 | Any value 118 |
  • 119 |
120 | 121 |

Returns:

122 |
    123 | 124 | boolean 125 | Whether t is a table 126 |
127 | 128 | 129 | 130 | 131 |
132 |
133 | 134 | util.table.map (t, fn) 135 |
136 |
137 | Applies a function to every element of a table and collects the results in a new table. 138 | Arguments passed to the function are Value, Key and the Table. 139 | 140 | 141 |

Parameters:

142 |
    143 |
  • t 144 | table 145 | A table of values 146 |
  • 147 |
  • fn 148 | function 149 | A function to apply to every value 150 |
  • 151 |
152 | 153 |

Returns:

154 |
    155 | 156 | table 157 | The return values of each call to fn 158 |
159 | 160 | 161 | 162 | 163 |
164 |
165 | 166 | util.table.for_each (t, fn) 167 |
168 |
169 | Applies a function to each element of a tabla and throws away the results. 170 | Arguments passed to the function are Value, Key and the Table. 171 | 172 | 173 |

Parameters:

174 |
    175 |
  • t 176 | table 177 | A table of values 178 |
  • 179 |
  • fn 180 | function 181 | A function to apply to every value 182 |
  • 183 |
184 | 185 | 186 | 187 | 188 | 189 |
190 |
191 | 192 | is_instance_of (instance, class) 193 |
194 |
195 | Returns whether an object is an instance of a certain class. 196 | 197 | 198 |

Parameters:

199 |
    200 |
  • instance 201 | table 202 | The object to check 203 |
  • 204 |
  • class 205 | string 206 | The class name the object might be an instance of 207 |
  • 208 |
209 | 210 |

Returns:

211 |
    212 | 213 | boolean 214 | Whether the object is an instance of the class 215 |
216 | 217 | 218 | 219 | 220 |
221 |
222 | 223 | util.deco.if_condition (condition) 224 |
225 |
226 | Given a condition, returns a function that returns 227 | either its first argument (if condition is true) 228 | or its second argument (if condition is false) 229 | 230 | 231 |

Parameters:

232 |
    233 |
  • condition 234 | Any Lua value 235 |
  • 236 |
237 | 238 |

Returns:

239 |
    240 | 241 | function 242 | One of the two functions described above 243 |
244 | 245 | 246 | 247 | 248 |
249 |
250 | 251 | util.value.not_nil (value, default) 252 |
253 |
254 | Checks whether a value is nil and returns a default value if so. 255 | 256 | 257 |

Parameters:

258 |
    259 |
  • value 260 | Value to check if is nil 261 |
  • 262 |
  • default 263 | Value to return if value param is nil 264 |
  • 265 |
266 | 267 |

Returns:

268 |
    269 | 270 | default if value is nil, value otherwise 271 |
272 | 273 | 274 | 275 | 276 |
277 |
278 | 279 | 280 |
281 |
282 |
283 | generated by LDoc 1.4.6 284 | Last updated 2019-05-06 01:56:35 285 |
286 |
287 | 288 | 289 | -------------------------------------------------------------------------------- /lualog/init.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | #Lualog class file 3 | @author Desvelao^^ 4 | ]] 5 | 6 | -- Module name: lualog 7 | -- Description: This is a simple logger for Lua. Create a simple logger for Lua with 8 | -- some configurations as show date, stylized tags and logger name. 9 | -- Author: Desvelao^^ 10 | -- Repository: https://github.com/Desvelao/lualog 11 | 12 | local colorizer = require'lualog.colorizer' 13 | local Inspect = require'lualog.inspect' 14 | local util = require'lualog.util' 15 | 16 | -- Create a logger instance what you can customize how elements are printed. 17 | -- @classmod Lualog 18 | local Lualog = {} 19 | Lualog.__class_name = 'lualog' 20 | Lualog.__inspect = Inspect.new() 21 | 22 | local unpack = unpack or table.unpack 23 | local pack2 = function(...) return {n=select('#', ...), ...} end 24 | local unpack2 = function(t) return unpack(t, 1, t.n) end 25 | 26 | --- Create a Lualog instance 27 | -- @tparam[opt={}] table options Options 28 | -- @tparam[opt=false] string|boolean options.datestring Date string for os.date(). If false, date is not logged 29 | -- @string[opt=""] options.tag Logger tag. 30 | -- @tparam[opt={}] {string,...} options.ignore_levels Array of levels ignored. 31 | -- @tparam[opt={}] {} options.styles Table that define styles. 32 | -- @tparam table options.table_inspect Table inspector config. 33 | -- @tparam[opt=false] boolean options.table_inspect.prettyfy Flag to pretty printing tables. 34 | -- @tparam[opt=true] boolean options.table_inspect.allow_tostring Flag to allow use table __tostring metamethod. 35 | -- @tparam[opt=0] boolean options.table_inspect.level_depth Depth level to inspect tables. 0 means no level limit. 36 | -- @tparam[opt={}] {function,...} options.plugins Plugins. 37 | -- @treturn Lualog 38 | function Lualog.new(options) 39 | options = options or {} 40 | options.table_inspect = options.table_inspect or {} 41 | 42 | local default = { 43 | style = '', 44 | datestring = false, -- '%I:%M:%S' 45 | tag = '', 46 | table_inspect = { 47 | prettyfy = false, 48 | allow_tostring = true, 49 | level_depth = 0 50 | } 51 | } 52 | 53 | -- Lualog instance initiation 54 | local log = { 55 | __options = { -- Options 56 | ignore_levels = {}, 57 | datestring = (type(datestring) == 'boolean' and datestring == true and '%I:%M:%S') or util.value.not_nil(options.datestring, default.datestring), 58 | tag = util.value.not_nil(options.tag,default.tag), 59 | table_inspect = { 60 | prettyfy = util.value.not_nil(options.table_inspect.prettyfy,default.table_inspect.prettyfy), 61 | allow_tostring = util.value.not_nil(options.table_inspect.allow_tostring,default.table_inspect.allow_tostring), 62 | level_depth = util.value.not_nil(options.table_inspect.level_depth,default.table_inspect.level_depth), 63 | }, 64 | }, 65 | __class_name = 'lualog-instance', -- Class anme 66 | __plugins = {} -- Plugins 67 | } 68 | setmetatable(log,{ 69 | __index = Lualog, 70 | --- Print elements with plugins applied 71 | -- @tparam Lualog t 72 | -- @param ... Elements to print 73 | __call = function(t,...) t:print(...) end 74 | }) 75 | 76 | -- Ignore levels 77 | options.ignore_levels = options.ignore_levels or {} 78 | for _,level in ipairs(options.ignore_levels) do 79 | log.__options.ignore_levels[level] = true 80 | end 81 | 82 | -- Plugins logging 83 | -- Return element 84 | log:use(function (el) 85 | return el 86 | end) 87 | 88 | -- Add a table inspector to lualog instance 89 | log.__inspect = Inspect.new{ 90 | prettyfy = log.__options.table_inspect.prettyfy, 91 | allow_tostring = log.__options.table_inspect.allow_tostring, 92 | level_depth = log.__options.table_inspect.level_depth 93 | } 94 | 95 | -- Add table-inspect plugin if options.plugins contains 'table-inspect' 96 | options.plugins = options.plugins or {} 97 | if(util.table.is(options.plugins) and util.table.includes(options.plugins,'table-inspect')) then 98 | log:use(function(el) 99 | if(not util.table.is(el)) then return end 100 | return log.__inspect:parse(el) 101 | end) 102 | for i,t in pairs(options.plugins) do 103 | if(t == 'table-inspect') then table.remove(options.plugins,i) end 104 | end 105 | end 106 | 107 | -- Add plugins from options.plugins 108 | if(util.table.is(options.plugins)) then 109 | util.table.for_each(options.plugins, 110 | function(plugin, index, t) 111 | log:use(plugin) 112 | end 113 | ) 114 | end 115 | 116 | -- Decorator to create log methods 117 | log.__log = function(style, tag) 118 | return function(...) 119 | if(not log.__options.ignore_levels[tag]) then 120 | print(log:__apply_plugins({ 121 | __class_name = 'lualog-log', 122 | style = style, 123 | tag = tag 124 | },...)) 125 | end 126 | return log 127 | end 128 | end 129 | 130 | -- Default log methods 131 | 132 | --- Log info 133 | -- @param ... Elements to print 134 | -- @function info 135 | -- @return self 136 | log.info = log.__log('blue','info') -- Info log 137 | 138 | --- Log warn 139 | -- @param ... Elements to print 140 | -- @function warn 141 | -- @return self 142 | log.warn = log.__log('yellow','warn') -- Warn log 143 | 144 | --- Log error 145 | -- @param ... Elements to print 146 | -- @function error 147 | -- @return self 148 | log.error = log.__log('red','error') -- Error log 149 | 150 | -- Custom log methods 151 | options.styles = options.styles or {} 152 | 153 | util.table.for_each(options.styles, 154 | function(opt_style, style, t) 155 | log[style] = log.__log(opt_style or default.style ,style) 156 | end 157 | ) 158 | 159 | log.__plugin_logtag = create_plugin(function(el) 160 | if(not util.table.is(el) or not util.is_instance_of(el, 'lualog-log') or not el.style or not el.tag) then return end 161 | if(not log.__options.ignore_levels[tag]) then 162 | local prefix = '' 163 | if(log.__options.datestring) then 164 | prefix = prefix .. os.date(log.__options.datestring) .. ' ' 165 | end 166 | if(#log.__options.tag > 0) then 167 | prefix = prefix .. log.__options.tag .. ' ' 168 | end 169 | return prefix..colorizer.ustr(el.style,el.tag) 170 | end 171 | end) 172 | 173 | return log 174 | end 175 | 176 | --- Function as `print` global function but apply your plugins for each element 177 | -- @param ... Element to print 178 | function Lualog:print(...) 179 | print(self:__apply_plugins(...)) 180 | end 181 | 182 | --- Add plugin/s to your lualog instance 183 | -- @tparam function ... Plugin/s to add to the queue 184 | -- @return self 185 | function Lualog:use(...) 186 | local plugins = pack2(...) 187 | for _,plugin in pairs(plugins) do 188 | if(self.__class_name ~= 'lualog-instance' or type(plugin) ~= 'function') then return end 189 | table.insert(self.__plugins, 1, create_plugin(plugin)) 190 | end 191 | return self 192 | end 193 | 194 | --- Return a text colorized with style. It's necesary use print function to see at console/terminal 195 | -- @string style Style to apply to the text 196 | -- @string text Text 197 | -- @treturn string 198 | function Lualog:paint(style,text) 199 | return colorizer.ustr(style,text) 200 | end 201 | 202 | -- Inspect a table with table inspector 203 | -- @tparam table t Table to inspect with inspector 204 | function Lualog:inspect(t) 205 | print(self.__inspect:parse(t)) 206 | end 207 | 208 | -- Create a Plugin 209 | function create_plugin(fn) 210 | return {render = fn, __class_name = 'lualog-plugin'} 211 | end 212 | 213 | -- Apply your plugins 214 | function Lualog:__apply_plugins(...) 215 | local elements = pack2(...) 216 | local elements_transformed = {} 217 | for i,element in pairs(elements) do 218 | local result = self.__plugin_logtag.render(element) 219 | if(not result) then 220 | for _,plugin in pairs(self.__plugins) do 221 | local result = plugin.render(element) 222 | if(result ~= nil) then 223 | elements_transformed[i] = tostring(result) 224 | break 225 | end 226 | end 227 | else 228 | elements_transformed[i] = tostring(result) 229 | end 230 | end 231 | return unpack2(elements_transformed) 232 | end 233 | 234 | return setmetatable(Lualog,{__call = function (t,...) return t.new(...) end}) -------------------------------------------------------------------------------- /docs/classes/Lualog.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Lualog - Simple logger for Lua 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 15 |
16 |
17 |
18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 60 | 61 |
62 | 63 |

Class Lualog

64 |

Create a logger instance what you can customize how elements are printed.

65 |

66 | 67 |

68 | 69 | 70 |

Methods

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 |
Lualog:new ([options={}])Create a Lualog instance
Lualog:info (...)Log info
Lualog:warn (...)Log warn
Lualog:error (...)Log error
Lualog:print (...)Function as print global function but apply your plugins for each element
Lualog:use (...)Add plugin/s to your lualog instance
Lualog:paint (style, text)Return a text colorized with style.
101 |

Metamethods

102 | 103 | 104 | 105 | 106 | 107 |
Lualog:__call (t, ...)Print elements with plugins applied
108 | 109 |
110 |
111 | 112 | 113 |

Methods

114 | 115 |
116 |
117 | 118 | Lualog:new ([options={}]) 119 |
120 |
121 | Create a Lualog instance 122 | 123 | 124 |

Parameters:

125 |
    126 |
  • options Options 127 |
      128 |
    • datestring 129 | string or boolean 130 | Date string for os.date(). If false, date is not logged 131 | (default false) 132 |
    • 133 |
    • tag 134 | string 135 | Logger tag. 136 | (default "") 137 |
    • 138 |
    • ignore_levels 139 | {string,...} 140 | Array of levels ignored. 141 | (default {}) 142 |
    • 143 |
    • styles 144 | {} 145 | Table that define styles. 146 | (default {}) 147 |
    • 148 |
    • table_inspect 149 | table 150 | Table inspector config. 151 |
    • 152 |
    • table_inspect.prettyfy 153 | boolean 154 | Flag to pretty printing tables. 155 | (default false) 156 |
    • 157 |
    • table_inspect.allow_tostring 158 | boolean 159 | Flag to allow use table __tostring metamethod. 160 | (default true) 161 |
    • 162 |
    • table_inspect.level_depth 163 | boolean 164 | Depth level to inspect tables. 0 means no level limit. 165 | (default 0) 166 |
    • 167 |
    • plugins 168 | {function,...} 169 | Plugins. 170 | (default {}) 171 |
    • 172 |
    173 |
174 | 175 |

Returns:

176 |
    177 | 178 | Lualog 179 | 180 | 181 | 182 |
183 | 184 | 185 | 186 | 187 |
188 |
189 | 190 | Lualog:info (...) 191 |
192 |
193 | Log info 194 | 195 | 196 |

Parameters:

197 |
    198 |
  • ... 199 | Elements to print 200 |
  • 201 |
202 | 203 |

Returns:

204 |
    205 | 206 | self 207 |
208 | 209 | 210 | 211 | 212 |
213 |
214 | 215 | Lualog:warn (...) 216 |
217 |
218 | Log warn 219 | 220 | 221 |

Parameters:

222 |
    223 |
  • ... 224 | Elements to print 225 |
  • 226 |
227 | 228 |

Returns:

229 |
    230 | 231 | self 232 |
233 | 234 | 235 | 236 | 237 |
238 |
239 | 240 | Lualog:error (...) 241 |
242 |
243 | Log error 244 | 245 | 246 |

Parameters:

247 |
    248 |
  • ... 249 | Elements to print 250 |
  • 251 |
252 | 253 |

Returns:

254 |
    255 | 256 | self 257 |
258 | 259 | 260 | 261 | 262 |
263 |
264 | 265 | Lualog:print (...) 266 |
267 |
268 | Function as print global function but apply your plugins for each element 269 | 270 | 271 |

Parameters:

272 |
    273 |
  • ... 274 | Element to print 275 |
  • 276 |
277 | 278 | 279 | 280 | 281 | 282 |
283 |
284 | 285 | Lualog:use (...) 286 |
287 |
288 | Add plugin/s to your lualog instance 289 | 290 | 291 |

Parameters:

292 |
    293 |
  • ... 294 | function 295 | Plugin/s to add to the queue 296 |
  • 297 |
298 | 299 |

Returns:

300 |
    301 | 302 | self 303 |
304 | 305 | 306 | 307 | 308 |
309 |
310 | 311 | Lualog:paint (style, text) 312 |
313 |
314 | Return a text colorized with style. It's necesary use print function to see at console/terminal 315 | 316 | 317 |

Parameters:

318 |
    319 |
  • style 320 | string 321 | Style to apply to the text 322 |
  • 323 |
  • text 324 | string 325 | Text 326 |
  • 327 |
328 | 329 |

Returns:

330 |
    331 | 332 | string 333 | 334 | 335 | 336 |
337 | 338 | 339 | 340 | 341 |
342 |
343 |

Metamethods

344 | 345 |
346 |
347 | 348 | Lualog:__call (t, ...) 349 |
350 |
351 | Print elements with plugins applied 352 | 353 | 354 |

Parameters:

355 |
    356 |
  • t 357 | Lualog 358 | 359 | 360 | 361 |
  • 362 |
  • ... 363 | Elements to print 364 |
  • 365 |
366 | 367 | 368 | 369 | 370 | 371 |
372 |
373 | 374 | 375 |
376 |
377 |
378 | generated by LDoc 1.4.6 379 | Last updated 2019-05-06 01:24:26 380 |
381 |
382 | 383 | 384 | -------------------------------------------------------------------------------- /docs/topics/readme.md.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Lualog - Simple logger for Lua 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 15 |
16 |
17 |
18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 62 | 63 |
64 | 65 | 66 |

Lualog

67 |

This is a simple logger for Lua.

68 | 69 |

Create a simple logger for Lua with some configurations as show date, stylized tags and logger name.

70 | 71 |

Not works with Luvit (due to Luvit custom require function)

72 | 73 |

Docs

74 | 75 |

See the documentation here.

76 | 77 |

Install

78 | 79 |

80 |

Using Luarocks

81 | 82 |
 83 | luarocks install lualog #global installation
 84 | luarocks install lualog --local # local installation
 85 | luarocks install lualog --tree lua_modules # current directory installation to lua_modules folder
 86 | 
87 | 88 |

89 |

Manual

90 | 91 |

Download this repository and copy the folder lualog and place it at root folder of your proyect.

92 | 93 |

Usage

94 | 95 |
 96 | -- Example
 97 | local Lualog = require'lualog'
 98 | 
 99 | local logger = Lualog.new{ -- you can use Lualog() instead Lualog.new
100 |     tag = 'My logger', -- a logger tag. default: ''
101 |     styles = {dev = 'yellow', crash = 'bgred'}, -- define custom methods and their style. default: {}
102 |     ignore_levels = {'dev'}, -- ignore levels (array). default: {}
103 |     datestring = '%H:%M:%S', -- date string for os.date(). default: false => Not show
104 |     table_inspect = { -- table-inspect plugin config
105 |         prettyfy = true, -- pretty print tables. default: false
106 |         allow_tostring = true, -- allow use table __tostring metamethod. default: true
107 |         level_depth = 0 -- max nested level to inspect. default: 0. 0 means no level limit
108 |     },
109 |     plugins = {'table-inspect'} -- add plugins to log items. table-inspect is a optional predefined plugin.
110 |                                -- You can add function plugins here too. See as define a plugin at Plugin section. default: {} => no plugins added.
111 | }
112 | -- Default logging methods
113 | logger.info('Hi. This\' a info message')
114 | logger.warn('This\' a warning message')
115 | logger.error('A fatal error just happened!')
116 | 
117 | -- Custom methods defined at initiation instance
118 | logger.dev('Ignored level. See ignore_levels table field') -- This log is ignored due to ignore_levels = {'dev'}
119 | logger.crash('A crash!','Oh no!') -- Support multiple args
120 | logger.error('My error').info('A log chainning method!') -- chain logging methods but they print at new lines
121 | 
122 | 123 | 124 |

Result:

125 | 126 |

Styles

127 | 128 |

Styles: customize the tag of your custom logging methods

129 | 130 |

If you want to create your custom logging methods, you can add a custom style:

131 | 132 |
    133 |
  • Color text: black, red, green, yellow, blue, magenta, cyan, white
  • 134 |
  • Background color: bgblack, bgred, bggreen, bgyellow, bgblue, bgmagenta, bgcyan, bgwhite
  • 135 |
  • Others styles: bold, underlined, reversed
  • 136 |
137 | 138 |

Add custom logging methods in options table when you init your instance with:

139 | 140 | 141 |
142 | local Lualog = require'lualog' -- equal to require('lualog')
143 | local logger = Lualog.new({
144 |     styles = {
145 |         dev='yellow', -- tag = style
146 |         my_log= 'red.bgblue' -- you can combine styles, red color and blue background for this method.
147 |     }
148 | })
149 | 
150 | -- and using them
151 | logger.dev('Dev log')
152 | logger.my_log('Other custom logging method')
153 | 
154 | 155 |

Result:

156 | 157 |

Combining styles

158 | 159 |

Methods of instance

160 | 161 |

When you create a Lualog instance, this has these methods (by default):

162 | 163 |

164 |

Default logging methods

165 | 166 |
    167 |
  • .info(...) - log method with style blue and tag info
  • 168 |
  • .warn(...) - log method with style yellow and tag warn
  • 169 |
  • .error(...) - log method with style red and tag error
  • 170 |
171 | 172 | 173 |

174 |

Advanced methods

175 | 176 |
    177 |
  • :print(...): function as print global function but apply your plugins. Or call your Lualog instance, logger in examples, as a function logger(...).
  • 178 |
179 | 180 |

*Tip: call your Lualog instance as function instead :print()*

181 | 182 |
183 | logger:print(a_number, a_boolean, a_string, a_table)
184 | -- or better, you can only call your logger as function! It's the same ;)
185 | logger(a_number, a_boolean, a_string, a_table) -- you got a custom print function with custom logging plugins!
186 | 
187 | 188 |

- :use(...): add plugins to queue. See Plugin section.

189 | 190 |
    191 |
  • :paint(style, text): return a colorized string. Use a print method to see it at console/terminal.

    192 | 193 |
    194 | local my_blue_string = logger:paint('blue','This string will be blue if is printed')
    195 | print(my_blue_string)
    196 | 
    197 |
  • 198 |
  • :inspect(table): inspect a table with table_inspect configuration defined at initiation the instance or default if not.

    199 | 200 |
    201 | local my_table = {
    202 |     str = 'string value',
    203 |     n = 1,
    204 |     bool = true,
    205 |     table = {
    206 |         nested_table = {
    207 |             my_nested_key = function() return 0 end
    208 |             }
    209 |         }
    210 |     }
    211 | 
    212 | logger:inspect(my_table) -- inspect a table with yor table_inspect config defined or use default values. No plugin is applied.
    213 | 
    214 | 215 |

    Log:

  • 216 |
217 | 218 |

Inspect table

219 | 220 |

Plugin

221 | 222 |

You can customize as an element is logged through to plugins. They are applied to logging methods and :print function.

223 | 224 | 225 |
226 | -- ... lualog instance defined
227 | function my_plugin_for_print_tables(element)
228 |     if(type(element) == 'table') then
229 |         -- if element is not a table, this plugin is ignored
230 |         -- and pass to next plugin in queue because didn't return a not-nil value
231 |         local result = ''
232 |         for key, value in pairs(element) do
233 |             result = result .. key .. ' = ' .. tostring(value) .. '\n'
234 |         end
235 |         return result -- return a not-nil value to print your element as you defined an this function/plugin
236 |     end
237 | end
238 | 
239 | logger:use(my_plugin_for_print_tables)
240 | 
241 | logger:print({
242 |     key1 = 'value1',
243 |     key2 = 'value2'
244 | })
245 | 
246 | -- Will print due to my_plugin_for_print_tables
247 | -- key1 = value1
248 | -- key2 = value2
249 | 
250 | -- or define a plugin with an anonymous function
251 | logger:use(function(element)
252 |     -- your plugin code
253 | end)
254 | 
255 | -- :use(...) accept multiple plugins definitions too
256 | logger:use(my_plugin,other_plugin,...)
257 | 
258 | 259 |

Log:

260 | 261 |

Custom Plugin

262 | 263 |

You can add so many plugins as you want. Last plugins added are checked first in the queue.

264 | 265 |

Note: you can use plugins to define how to print your custom objects what are instance of a custom class.

266 | 267 |

Order what your plugin is added is important. Last plugins added are checked first. If a plugin return a not-nil value, then that return value is logged. Of this way you can customize your logs.

268 | 269 | 270 |

Issues requiring the module

271 | 272 |

Be sure that module is accessible to path of LUA_PATH enviorement variable.

273 | 274 |

*Note: Your LUA_PATH should contain ./?.lua;./?/init.lua;* if you place lualog folder in your project root folder.

275 | 276 |

Note: You can check your LUA_PATH adding to at beginning of your root script:

277 | 278 | 279 |
280 | -- Print your your LUA_PATH that Lua uses to require modules.
281 | print(package.path)
282 | 
283 | 284 | 285 |

You can extends your LUA_PATH at beginning to your file when you init it or add the path where lualog is to your LUA_PATH enviorement variable.

286 | 287 | 288 |
289 | -- Extends your LUA_PATH when you init your script/program
290 | package.path = package.path .. './?.lua;./?/init.lua;' .. 'path/to/your/modules/?.lua'
291 | 
292 | 293 | 294 |

License

295 |

MIT

296 | 297 | 298 |
299 |
300 |
301 | generated by LDoc 1.4.6 302 | Last updated 2019-05-06 01:56:35 303 |
304 |
305 | 306 | 307 | --------------------------------------------------------------------------------