{"id":13413293,"url":"https://github.com/aphistic/gomol","last_synced_at":"2025-03-14T19:31:47.810Z","repository":{"id":57480360,"uuid":"41634108","full_name":"aphistic/gomol","owner":"aphistic","description":"Gomol is a library for structured, multiple-output logging for Go with extensible logging outputs","archived":true,"fork":false,"pushed_at":"2019-03-14T03:15:36.000Z","size":185,"stargazers_count":19,"open_issues_count":3,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-25T05:23:03.047Z","etag":null,"topics":["go","gomol","logging","structured-logging"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aphistic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-30T15:51:46.000Z","updated_at":"2024-01-03T14:12:43.000Z","dependencies_parsed_at":"2022-09-26T17:41:40.862Z","dependency_job_id":null,"html_url":"https://github.com/aphistic/gomol","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aphistic%2Fgomol","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aphistic%2Fgomol/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aphistic%2Fgomol/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aphistic%2Fgomol/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aphistic","download_url":"https://codeload.github.com/aphistic/gomol/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243635329,"owners_count":20322921,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","gomol","logging","structured-logging"],"created_at":"2024-07-30T20:01:37.011Z","updated_at":"2025-03-14T19:31:47.787Z","avatar_url":"https://github.com/aphistic.png","language":"Go","readme":"gomol\n=====\n\n[![GoDoc](https://godoc.org/github.com/aphistic/gomol?status.svg)](https://godoc.org/github.com/aphistic/gomol)\n[![Build Status](https://travis-ci.org/aphistic/gomol.svg?branch=master)](https://travis-ci.org/aphistic/gomol)\n[![Code Coverage](https://img.shields.io/codecov/c/github/aphistic/gomol.svg)](http://codecov.io/github/aphistic/gomol?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/aphistic/gomol)](https://goreportcard.com/report/github.com/aphistic/gomol)\n\nGomol (Go Multi-Output Logger) is an MIT-licensed structured logging library for Go.  Gomol grew\nfrom a desire to have a structured logging library that could write to any number of outputs\nwhile also keeping a small in-band footprint.\n\nFeatures\n========\n\n* Attach meta-data to each log message with attributes\n* Multiple outputs at the same time\n* Pluggable Logger interface\n* Asynchronous logging so slow loggers won't slow down your application\n\nInstallation\n============\n\nGomol can also be installed the standard way for Go:\n\n    go get github.com/aphistic/gomol\n    ...\n    import \"github.com/aphistic/gomol\"\n\nVendoring is recommended!\n\nLoggers\n=======\n\nGomol has a growing list of supported logging formats.  The known loggers are listed\nbelow.  If you have a logger you've written to support gomol and you'd like to add it\nto this list please either submit a pull request with the updated document or let me\nknow and I can add it!\n\n* **Console** - https://github.com/aphistic/gomol-console\n* **Graylog Extended Log Format (GELF)** - https://github.com/aphistic/gomol-gelf\n* **io.Writer** - https://github.com/aphistic/gomol-writer\n* **JSON** - https://github.com/aphistic/gomol-json\n* **Loggly** - https://github.com/aphistic/gomol-loggly\n\nOther Usages\n============\n\nIn addition to the loggers listed above, gomol can be used with other projects as well.\n\n* **negroni-gomol** (https://github.com/aphistic/negroni-gomol) - Negroni logging middleware\n\tusing gomol.\n\nExamples\n========\n\nFor brevity a lot of error checking has been omitted, be sure you do your checks!\n\nThis is a super basic example of adding a number of loggers and then logging a few messages:\n\n```go\npackage main\n\nimport (\n\t\"github.com/aphistic/gomol\"\n\tgc \"github.com/aphistic/gomol-console\"\n\tgg \"github.com/aphistic/gomol-gelf\"\n)\n\nfunc main() {\n\t// Add a console logger\n\tconsoleCfg := gc.NewConsoleLoggerConfig()\n\tconsoleLogger, _ := gc.NewConsoleLogger(consoleCfg)\n\t// Set the template to display the full message, including\n\t// attributes.\n\tconsoleLogger.SetTemplate(gc.NewTemplateFull())\n\tgomol.AddLogger(consoleLogger)\n\n\t// Add a GELF logger\n\tgelfCfg := gg.NewGelfLoggerConfig()\n\tgelfCfg.Hostname = \"localhost\"\n\tgelfCfg.Port = 12201\n\tgelfLogger, _ := gg.NewGelfLogger(gelfCfg)\n\tgomol.AddLogger(gelfLogger)\n\n\t// Set some global attrs that will be added to all\n\t// messages automatically\n\tgomol.SetAttr(\"facility\", \"gomol.example\")\n\tgomol.SetAttr(\"another_attr\", 1234)\n\n\t// Configure gomol to add the filename and line number that the\n\t// log was generated from, the internal sequence number to help\n\t// with ordering events if your log system doesn't support a\n\t// small enough sub-second resolution, and set the size of the\n\t// internal queue (default is 10k messages).\n\tcfg := gomol.NewConfig()\n\tcfg.FilenameAttr = \"filename\"\n\tcfg.LineNumberAttr = \"line\"\n\tcfg.SequenceAttr = \"sequence\"\n\tcfg.MaxQueueSize = 50000\n\tgomol.SetConfig(cfg)\n\n\t// Initialize the loggers\n\tgomol.InitLoggers()\n\tdefer gomol.ShutdownLoggers()\n\n\t// Create a channel on which to receive internal (asynchronous)\n\t// logger errors. This is optional, but recommended in order to\n\t// determine when logging may be dropping messages.\n\tch := make(chan error)\n\n\tgo func() {\n\t\t// This consumer is expected to be efficient as writes to \n\t\t// the channel are blocking. If this handler is slow, the\n\t\t// user should add a buffer to the channel, or manually\n\t\t// queue and batch errors for processing.\n\n\t\tfor err := range ch {\n\t\t\tfmt.Printf(\"[Internal Error] %s\\n\", err.Error())\n\t\t}\n\t}()\n\n\tgomol.SetErrorChan(ch)\n\n\t// Log some debug messages with message-level attrs\n\t// that will be sent only with that message\n\tfor idx := 1; idx \u003c= 10; idx++ {\n\t\tgomol.Dbgm(gomol.NewAttrsFromMap(map[string]interface{}{\n\t\t\t\"msg_attr1\": 4321,\n\t\t}), \"Test message %v\", idx)\n\t}\n}\n```\n\nFallback Logger\n===============\n\nOne feature gomol supports is the concept of a fallback logger.  In some cases a logger\nmay go unhealthy (a logger to a remote server, for example) and you want to log to a\ndifferent logger to ensure log messages are not lost.  The SetFallbackLogger method is\navailable for these such instances.\n\nA fallback logger will be triggered if any of the primary loggers goes unhealthy even if\nall others are fine. It's recommended the fallback logger not be added to the primary\nloggers or you may see duplicate messages.  This does mean if multiple loggers are added\nas primary loggers and just one is unhealthy the fallback logger logger will be triggered.\n\nTo add a fallback logger there are two options.  One is to use the default gomol instance\n(`gomol.SetFallbackLogger()`) and the other is to use the method on a Base instance:\n\n```go\nimport (\n\t\"github.com/aphistic/gomol\"\n\t\"github.com/aphistic/gomol-console\"\n\t\"github.com/aphistic/gomol-json\"\n)\n\nfunc main() {\n\t// Create a logger that logs over TCP using JSON\n\tjsonCfg := gomoljson.NewJSONLoggerConfig(\"tcp://192.0.2.125:4321\")\n\t// Continue startup even if we can't connect initially\n\tjsonCfg.AllowDisconnectedInit = true\n\tjsonLogger, _ := gomoljson.NewJSONLogger(jsonCfg)\n\tgomol.AddLogger(jsonLogger)\n\n\t// Create a logger that logs to the console\n\tconsoleCfg := gomolconsole.NewConsoleLoggerConfig()\n\tconsoleLogger, _ := gomolconsole.NewConsoleLogger(consoleCfg)\n\n\t// Set the fallback logger to the console so if the\n\t// TCP JSON logger is unhealthy we still get logs\n\t// to stdout.\n\t_ = gomol.SetFallbackLogger(consoleLogger)\n\n\tgomol.InitLoggers()\n\tdefer gomol.ShutdownLoggers()\n\n\tgomol.Debug(\"This is my message!\")\n}\n```\n","funding_links":[],"categories":["Logging 日志库","日志记录","Logging","日志","Relational Databases","\u003cspan id=\"日志-logging\"\u003e日志 Logging\u003c/span\u003e"],"sub_categories":["SQL 查询语句构建库","检索及分析资料库","Advanced Console UIs","Search and Analytic Databases","交流","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faphistic%2Fgomol","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faphistic%2Fgomol","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faphistic%2Fgomol/lists"}