|
| 1 | +# LoggingTelegram ✈️ |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +Welcome 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). Inspired by and forked from [LoggingSlack](https://github.com/wlisac/swift-log-slack). |
| 8 | + |
| 9 | +## Usage 📝 |
| 10 | + |
| 11 | +### Setup Telegram Bot |
| 12 | + |
| 13 | +LoggingTelegram uses [Telegram Bot API](https://core.telegram.org/bots/api) to send log messages to any Telegram chat. |
| 14 | + |
| 15 | +You 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). |
| 16 | + |
| 17 | +You need to pass the token to `TelegramLogHandler` to set it up. |
| 18 | + |
| 19 | +### Get a Target Chat ID |
| 20 | + |
| 21 | +You 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). |
| 22 | + |
| 23 | +You can create a group chat with `TelegramGroup.id(_:Int)`, a channel with `TelegramChannel.id(_:Int)` and a user chat with `TelegramUser.id(_:Int)`. |
| 24 | + |
| 25 | +Alternatively, you can create a channel by its name: `TelegramChannel.name(_:String)`, remember to drop '@' prefix. |
| 26 | + |
| 27 | +### @someone in a Group Chat or a channel |
| 28 | + |
| 29 | +You can mention someone in a group chat or a channel, simply by the various `.mentioning()` APIs, listed below: |
| 30 | + |
| 31 | +```swift |
| 32 | +var chat = TelegramChannel.name("test") |
| 33 | + |
| 34 | +// By TelegramUser |
| 35 | +let users = [1,2].map { TelegramUser.id($0) } |
| 36 | +chat = chat.mentioning(users) // Returns a new TelegramChannel instance |
| 37 | +// You can use his username to mention a user |
| 38 | +let user = TelegramUser.name("testme") |
| 39 | +chat = chat.mentioning(user) |
| 40 | + |
| 41 | +// By TelegramRawId (A more flexible alias of the "TelegramUser" version) |
| 42 | +chat = chat.mentioning(.id(3)) |
| 43 | +chat = chat.mentioning([.name("newtest"), .id(4)]) |
| 44 | + |
| 45 | +// By User ID |
| 46 | +chat = chat.mentioning(5) |
| 47 | +chat = chat.mentioning([6,7,8]) |
| 48 | + |
| 49 | +// By Username |
| 50 | +chat = chat.mentioning("test 1") |
| 51 | +chat = chat.mentioning(["test 2", "test 3"]) |
| 52 | +``` |
| 53 | + |
| 54 | +In brief, the chaining style is recommended to create a chat: |
| 55 | + |
| 56 | +```swift |
| 57 | +TelegramChannel.name("test").mentioning([.id(1), .name("2"), .id(3)]) |
| 58 | +// or |
| 59 | +TelegramChannel.name("test").mentioning([1, 3]).mentioning("2") |
| 60 | +``` |
| 61 | + |
| 62 | +### Bootstrap SwiftLog |
| 63 | + |
| 64 | +LoggingTelegram is intended to be used as a secondary logging backend to send urgent log messages directly to Telegram. |
| 65 | + |
| 66 | +You can use SwiftLog's `MultiplexLogHandler` to setup LoggingTelegram with another logging backend. |
| 67 | + |
| 68 | +```swift |
| 69 | +import Logging |
| 70 | +import LoggingTelegram |
| 71 | + |
| 72 | +let channel = TelegramChannel.name("test").mentioning([1, 3]).mentioning("2") |
| 73 | + |
| 74 | +LoggingSystem.bootstrap { label in |
| 75 | + MultiplexLogHandler([ |
| 76 | + // Setup TelegramLogHandler with your API Token and Chat instance |
| 77 | + TelegramLogHandler(label: label, token: "Your Bot Token Here", chat: channel), |
| 78 | + |
| 79 | + // Setup the standard logging backend to enable console logging |
| 80 | + StreamLogHandler.standardOutput(label: label), |
| 81 | + |
| 82 | + // Setup multiple TelegramLogHandlers |
| 83 | + // TelegramLogHandler(label: label, token: "Your (Another) Bot Token Here", chat: TelegramGroup.id(123)) |
| 84 | + ]) |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### Using a Logger |
| 89 | + |
| 90 | +You can now use SwiftLog as usual and critical log messages are sent directly to Telegram. Test code as below. |
| 91 | + |
| 92 | +```swift |
| 93 | +import Logging |
| 94 | + |
| 95 | +let logger = Logger(label: "com.example.ExampleApp.main") |
| 96 | + |
| 97 | +logger.critical("Oops, something went wrong!") |
| 98 | +``` |
| 99 | + |
| 100 | +### Logger Output |
| 101 | + |
| 102 | +The 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: |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +```plain |
| 107 | +2020-04-07T16:05:25+0800 critical: Oops, something went wrong! |
| 108 | +``` |
| 109 | + |
| 110 | +### Log Level |
| 111 | + |
| 112 | +Only messages of [log level](https://github.com/apple/swift-log#log-levels) `critical` are sent to Telegram by default. |
| 113 | + |
| 114 | +You can adjust the log level minimal for a specific `TelegramLogHandler` by setting its `logLevel` property, or initializing with a `level` property. |
| 115 | + |
| 116 | +```swift |
| 117 | +var handler = TelegramLogHandler(label: "test", token: "Your Bot Token Here", chat: TelegramGroup.id(123), level: .error) |
| 118 | +handler.logLevel = .info // Unrecommended! Low log levels may burden the server and abuse the Telegram function. |
| 119 | +``` |
| 120 | + |
| 121 | +You can change the default for all `TelegramLogHandler`s by changing the value of `telegramLogDefaultLevel`. |
| 122 | + |
| 123 | +```swift |
| 124 | +telegramLogDefaultLevel = .error |
| 125 | +``` |
| 126 | + |
| 127 | +Remember, the `TelegramLogHandler.logLevel` property has a priority to `telegramLogDefaultLevel`. |
| 128 | + |
| 129 | +### Mute Setting |
| 130 | + |
| 131 | +To not disturb subscribers, you may need to mute the message. You can pass a `mute: Bool` to the initializer to make it silent: |
| 132 | + |
| 133 | +```swift |
| 134 | +TelegramLogHandler(label: label, token: "Your Bot Token Here", chat: channel, mute: true) |
| 135 | +``` |
| 136 | + |
| 137 | +## Installation 📦 |
| 138 | + |
| 139 | +LoggingTelegram requires Xcode 11 or a Swift 5.2 toolchain with the Swift Package Manager. |
| 140 | + |
| 141 | +Add the LoggingTelegram package as a dependency to your `Package.swift` file. |
| 142 | + |
| 143 | +```swift |
| 144 | +.package(url: "https://github.com/stevapple/swift-log-telegram.git", from: "0.0.1") |
| 145 | +``` |
| 146 | + |
| 147 | +Add LoggingTelegram to your target's dependencies. |
| 148 | + |
| 149 | +```swift |
| 150 | +.target(name: "Example", dependencies: ["LoggingTelegram"]) |
| 151 | +``` |
| 152 | + |
| 153 | +## Attention ⚠️ |
| 154 | + |
| 155 | +You 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. |
0 commit comments