{"id":30065870,"url":"https://github.com/stevapple/swift-log-telegram","last_synced_at":"2025-08-08T06:39:42.907Z","repository":{"id":53092593,"uuid":"253732261","full_name":"stevapple/swift-log-telegram","owner":"stevapple","description":"Send SwiftLog messages to any Telegram chat.","archived":false,"fork":false,"pushed_at":"2023-05-27T12:55:05.000Z","size":50,"stargazers_count":31,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-02T17:00:09.663Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","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/stevapple.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":"2020-04-07T08:25:46.000Z","updated_at":"2025-04-09T09:25:57.000Z","dependencies_parsed_at":"2022-09-12T17:43:08.368Z","dependency_job_id":null,"html_url":"https://github.com/stevapple/swift-log-telegram","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/stevapple/swift-log-telegram","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevapple%2Fswift-log-telegram","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevapple%2Fswift-log-telegram/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevapple%2Fswift-log-telegram/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevapple%2Fswift-log-telegram/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stevapple","download_url":"https://codeload.github.com/stevapple/swift-log-telegram/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevapple%2Fswift-log-telegram/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269378439,"owners_count":24407314,"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","status":"online","status_checked_at":"2025-08-08T02:00:09.200Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-08-08T06:39:42.097Z","updated_at":"2025-08-08T06:39:42.886Z","avatar_url":"https://github.com/stevapple.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LoggingTelegram ✈️\n\n![Swift](https://img.shields.io/badge/Swift-5.2-orange.svg)\n![Release](https://img.shields.io/github/v/tag/stevapple/swift-log-telegram?label=release\u0026logo=github)\n![License](https://img.shields.io/github/license/stevapple/swift-log-telegram)\n\nWelcome to **LoggingTelegram** – a logging backend for [SwiftLog](https://github.com/apple/swift-log) that sends log messages by a [Telegram Bot](https://core.telegram.org/bots).\n\nInspired by and forked from [LoggingSlack](https://github.com/wlisac/swift-log-slack). \n\n\n## Installation 📦\n\nLoggingTelegram requires Xcode 11 or a Swift 5.2 toolchain with the Swift Package Manager. \n\nAdd the LoggingTelegram package as a dependency to your `Package.swift` file.\n\n```swift\n.package(url: \"https://github.com/stevapple/swift-log-telegram.git\", from: \"0.0.1\")\n```\n\nAdd LoggingTelegram to your target's dependencies.\n\n```swift\n.target(name: \"Example\", dependencies: [\"LoggingTelegram\"])\n```\n\nThen start using by:\n\n```swift\nimport LoggingTelegram\n```\n\n## Usage 📝\n\n### Setup a Telegram Bot\n\nLoggingTelegram uses [Telegram Bot API](https://core.telegram.org/bots/api) to send log messages to any Telegram chat.\n\nYou can chat with [BotFather](https://t.me/BotFather) to create a new bot and get its API token. The config methods are described [here](https://core.telegram.org/bots#6-botfather). \n\nYou need to pass the token to `TelegramLogHandler.init` to set it up. \n\n### Get a Target Chat ID\n\nYou can access a chat by its ID. There are various ways to get the ID of a chat, some are discussed in [this thread](https://stackoverflow.com/questions/31078710/how-to-obtain-telegram-chat-id-for-a-specific-user). \n\nYou can create a group chat with `TelegramGroup.id(_:Int)`, a channel with `TelegramChannel.id(_:Int)` and a user chat with `TelegramUser.id(_:Int)`.\n\nAlternatively, you can create a channel by its name:  `TelegramChannel.name(_:String)`, remember to drop `@` prefix.\n\n### Bootstrap SwiftLog\n\nLoggingTelegram is intended to be used as a secondary logging backend to send urgent log messages directly to Telegram.\n\nYou can use SwiftLog's `MultiplexLogHandler` to setup LoggingTelegram with another logging backend.\n\n```swift\nimport Logging\nimport LoggingTelegram\n\nlet channel = TelegramChannel.name(\"test\").mentioning([1, 3]).mentioning(\"2\")\n\nLoggingSystem.bootstrap { label in\n    MultiplexLogHandler([\n        // Setup TelegramLogHandler with your API Token and Chat instance\n        TelegramLogHandler(label: label, token: \"Your Bot Token Here\", chat: channel),\n        \n        // Setup the standard logging backend to enable console logging\n        StreamLogHandler.standardOutput(label: label),\n        \n        // Setup multiple TelegramLogHandlers\n        // TelegramLogHandler(label: label, token: \"Your (Another) Bot Token Here\", chat: TelegramGroup.id(123))\n    ])\n}\n```\n\nSince SwiftLog does not support parallel logs and that network connection takes time, loggers after the `TelegramLogHandler` may need to wait for its HTTP request sometimes. \n\nIf you want to get the precise timestamp at the console, you may put `TelegramLogHandler` after `StreamLogHandler`:\n\n```swift\nimport Logging\nimport LoggingTelegram\n\nlet channel = TelegramChannel.name(\"test\").mentioning([1, 3]).mentioning(\"2\")\n\nLoggingSystem.bootstrap { label in\n    MultiplexLogHandler([\n        StreamLogHandler.standardOutput(label: label),\n        TelegramLogHandler(label: label, token: \"Your Bot Token Here\", chat: channel),\n        TelegramLogHandler(label: label, token: \"Your (Another) Bot Token Here\", chat: TelegramGroup.id(123))\n    ])\n}\n```\n\n### Create and Use a Logger\n\nYou can now use SwiftLog as usual and critical log messages are sent directly to Telegram. Test code as below. \n\n```swift\nimport Logging\n\nlet logger = Logger(label: \"com.example.ExampleApp.main\")\n\nlogger.critical(\"Oops, something went wrong!\")\n```\n\n### Check Logger Outputs\n\nThe logger will send a Telegram message as a bot (if the level matches) and a console message since both logging backends were setup. The example above gives the following outputs: \n\n![Telegram message](Assets/message.png)\n\n```plain\n2020-04-07T16:05:25+0800 critical: Oops, something went wrong!\n```\n\n### Configure Log Level\n\nOnly messages of [log level](https://github.com/apple/swift-log#log-levels) `critical` are sent to Telegram by default.\n\nYou can adjust the minimal log level for a specific `TelegramLogHandler` by setting its `logLevel` property, or initializing with a `level` property. \n\n```swift\nvar handler = TelegramLogHandler(label: \"test\", token: \"Your Bot Token Here\", chat: TelegramGroup.id(123), level: .error)\n// Unrecommended! Low log levels may burden the server and abuse the Telegram function\nhandler.logLevel = .info\n```\n\nYou can change the default for all `TelegramLogHandler`s by changing the value of `telegramLogDefaultLevel`. \n\n```swift\ntelegramLogDefaultLevel = .error\n```\n\nRemember, the `TelegramLogHandler.logLevel` property has a priority to `telegramLogDefaultLevel`. \n\n### @Someone in a Group Chat or a Channel\n\nYou can mention someone in a group chat or a channel, simply by the various `.mentioning()` APIs, listed below:\n\n```swift\nimport LoggingTelegram\n\nvar chat = TelegramChannel.name(\"test\")\n\n// By TelegramUser\nlet users = [1,2].map { TelegramUser.id($0) }\nchat = chat.mentioning(users) // Returns a new TelegramChannel instance\n// You can use his username to mention a user\nlet user = TelegramUser.name(\"testme\")\nchat = chat.mentioning(user)\n\n// By TelegramRawId (A more flexible alias of the \"TelegramUser\" version)\nchat = chat.mentioning(.id(3))\nchat = chat.mentioning([.name(\"newtest\"), .id(4)])\n\n// By User ID\nchat = chat.mentioning(5)\nchat = chat.mentioning([6,7,8])\n\n// By Username\nchat = chat.mentioning(\"test 1\")\nchat = chat.mentioning([\"test 2\", \"test 3\"])\n```\n\nIn brief, the chaining style is recommended when creating a chat:\n\n```swift\nimport LoggingTelegram\n\nTelegramChannel.name(\"test\").mentioning([.id(1), .name(\"2\"), .id(3)])\n// or\nTelegramChannel.name(\"test\").mentioning([1, 3]).mentioning(\"2\")\n```\n\n### Mute the Messages\n\nTo not disturb subscribers, you may need to mute the message. You can pass a `mute: Bool` to the initializer to make it silent:\n\n```swift\nTelegramLogHandler(label: label, token: \"Your Bot Token Here\", chat: channel, mute: true)\n```\n\nThe mentioned users won't be muted. \n\n## Attention ⚠️\n\nYou should know what you're doing though this package. Do not use it at client-side to protect the subscribers' info and your Bot Token. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevapple%2Fswift-log-telegram","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstevapple%2Fswift-log-telegram","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevapple%2Fswift-log-telegram/lists"}