使用 a9s LogMe
将应用日志流式传输到 LogMe
要在应用中使用 a9s LogMe,请创建服务实例并将服务实例绑定到应用中。有关管理服务实例的详细信息,请参见使用 cf CLI 管理服务实例。
查看服务产品
磁贴安装完成后,运行 cf marketplace
并查看 logme
服务产品及其服务计划。
$ cf marketplace
Getting services from marketplace in org test / space test as admin...
OK
service plans description
logme logme-xs,logme-m This is the anynines LogMe service.
创建服务实例
使用任意 {instance_name}
提供 LogMe 实例。
```shell tab="Template" cf create-service {service_name} {plan_name} {instance_name}
```shell tab="Example"
cf create-service logme logme-xs my-logme-service
创建服务实例可能需要几分钟的时间,具体取决于您的基础架构和服务代理使用情况。
运行 cf services
命令可以查看创建状态。此命令将显示所有服务实例的列表。要查看特定服务实例的状态,请运行 cf service {instance_name}
。
将 Logme 服务绑定到应用
创建 LogMe 服务实例后,将 LogMe 服务绑定到要为其流式传输日志的应用。
```shell tab="Template" cf bind-service {app_name} {instance_name}
```shell tab="Example"
cf bind-service my-app my-logme-service
重新载入或重新启动应用
要使应用能够访问服务实例,请运行 cf restage
或 cf restart
来重新载入或重新启动应用。
查看应用日志
请按以下步骤查看应用日志:
- 使用
cf service my-logme-service
获取仪表盘 URL:
Service instance:my-logme-service
Service:logme
Bound apps:my-app
Tags:
Plan:logme-xs
description: This is the anynines LogMe service.
Documentation url:https://a9s-logme-dashboards.your-domain.com/service-instance/a89f3114-5e77-40a5-b3b0-34a9741f3cd5
Dashboard:
Last Operation
Status:create succeeded
Message:
Started:2017-07-22T17:10:21Z
Updated:2017-07-22T17:13:26Z
- 访问仪表盘 URL 并通过“使用 WebKey 登录”方法在重定向的页面上进行身份验证:
- 单击 **授权**以批准授权请求:
- 在出现的仪表盘中,指定
Index name or pattern
以及Time-field name
。在Index name or pattern
中,您可以使用建议值。
在Time-field name
中,请使用唯一可用的值@timestamp
。
- 单击 **创建**来应用设置。
应用日志显示在仪表盘**发现**选项卡上:
将 a9s 服务日志流式传输到 LogMe
要使用 LogMe 服务实例监控其他服务实例,请先创建 as LogMe 服务实例。
创建服务密钥
LogMe 服务例示后,为服务实例创建新密钥:
```shell tab="Template" cf create-service-key {instance_name} {service_key}
```shell tab="Example"
$ cf create-service-key my-logme-service key1
$ cf service-key my-logme-service key1
{
"host": "syslog://d37f7da-logstash.service.dc1.consul:514",
"password": "a9sfd6e0d814e78c903290ebb5a7207b20c5f0a2653",
"username": "a9sed20b19c769f0804bc68b97d02cba86e9c3a0379"
}
更新服务
使用 cf update-service
命令与 -c flag 可以将 Syslog 流式传输到第三方服务。在这种情况下,该命令需要一个包含 syslog
键的 JSON 字符串。因此,需要将 cf service-key
命令返回的 URL 作为 syslog
键的值。
$ cf update-service service-instance-to-monitor \
-c '{"syslog": ["d37f7da-logstash.service.dc1.consul:514"]}'
查看服务日志
请按以下步骤查看服务日志:
-
使用
cf service my-logme-service
获取仪表盘 URL。Service instance:my-logme-service Service:logme Bound apps:my-app Tags: Plan:logme-xs description: This is the anynines LogMe service. Documentation url:https://a9s-logme-dashboards.your-domain.com/service-instance/a89f3114-5e77-40a5-b3b0-34a9741f3cd5 Dashboard: Last Operation Status:create succeeded Message: Started:2017-07-22T17:10:21Z Updated:2017-07-22T17:13:26Z
-
访问仪表盘 URL 并使用 Cloud Foundry 凭证在重定向的页面上进行身份验证:
-
单击 **授权**以批准授权请求:
-
随即出现仪表盘。指定
Index name or pattern
以及Time-field name
。在Index name or pattern
中,您可以使用建议值。在
Time-field name
中,请使用唯一可用的值@timestamp
。 -
单击 **创建**来应用设置。
服务日志显示在仪表盘的“发现”选项卡上:
停止应用日志到 LogMe 的流式传输
按照以下步骤,停止应用日志到 LogMe 的流式传输。
列出可用服务
运行 cf services
以列出可用的服务实例,并获取要删除的服务实例名称。
$ cf services
Getting services in org test / space test as admin...
OK
name service plan bound apps last operation
my-logme-service logme logme-xs logme-app create succeeded
在此示例中,将服务实例 my-logme-service
绑定到 logme-app
应用上。
解绑服务实例
如果您的 LogMe 服务实例已绑定到某个应用,则需要先将其解绑。
```shell tab="Template" cf unbind-service {app_name} {instance_name}
```shell tab="Example"
cf unbind-service logme-app my-logme-service
删除服务实例
运行 cf delete-service INSTANCE-NAME
删除服务实例:
```shell tab="Template" cf delete-service {instance_name}
```shell tab="Example"
cf delete-service my-logme-service
删除服务可能需要几分钟的时间。删除服务会删除相应的基础架构资源。运行 cf services
命令可以查看删除状态。
自定义参数
数据保留
要清理旧的 Elasticsearch 索引,可以定期运行 Curator 工具。默认情况下,该工具会删除超过 *30 天*的索引。
您可以使用自定义参数 curator_retention_unit
和 curator_retention_period
覆盖该配置。例如:
cf create-service a9s-logme logme-single-small my-logme-service -c '{ "curator_retention_unit": "days", "curator_retention_period":90 }'
cf update-service my-logme-service -c '{ "curator_retention_unit": "hours", "curator_retention_period":3 }'
curator_retention_unit
的可选值:seconds
、minutes
、hours
、days
、weeks
、months
、years
。curator_retention_period
应使用大于零的正整数值。
停止服务日志到 LogMe 的流式传输
覆盖您的服务配置
用空字符串更新您的服务配置的 syslog
键的值,停止服务实例日志到 LogMe 实例的流式传输。
```shell tab="Template" cf update-service {instance_name} -c '{"syslog": [""]}'
```shell tab="Example"
cf update-service service-instance-to-monitor -c '{"syslog": [""]}'
删除服务密钥和实例
您可能想要删除不再使用的服务密钥和服务实例。
```shell tab="Template" cf delete-service-key {instance_name} {service_key} cf delete-service {instance_name}
```shell tab="Example"
cf delete-service-key my-logme-service key1
cf delete-service my-logme-service
删除服务可能需要几分钟的时间。删除服务会删除相应的基础架构资源。运行 cf services
命令可以查看删除状态。
处理堆栈跟踪
Java 堆栈跟踪通常包含多行内容,但是 Logstash 和 Syslog 协议不支持开箱即用的多行日志事件。相反,它们将每一行作为一个单独的事件存储在 Elasticsearch 中。通过将换行字符 \n
编码为 \u2028
(行分隔符)来防止这种情况。这使 Syslog 和 Logstash 能够将堆栈跟踪作为日志事件读取,但是当将其保存到 Elasticsearch 时,它将转换为换行字符。
调整应用的日志配置以使用编码的换行字符 \u2028
代替默认的 \n
行分隔符。
Java Spring 配置示例
本章节展示了如何使用应用属性或 YAML/XML 文件来配置多日志事件的日志记录设置。
添加以下代码段到应用的日志记录设置:
%replace(%xException){'\n','\u2028'}%nopex
这个代码段将堆栈跟踪中出现的每个 \n
转换为 \u2028
(行分隔符),并使用 %nopex
模式过滤日志中的原始文本。
application.properties
的示例配置:
在本例中,application.properties
文件中的 logging.pattern.console
属性被设置来记录日期、时间和日志消息,后面跟着一个换行符,如下所示。
通过在换行符前添加给定的代码片段,在异常消息中编码换行符。这将编码应用于异常的堆栈跟踪,但不修改其他日志事件。
```java tab="Original logging config" logging.pattern.console=%d{yyyy-MM-dd HHss} - %msg%n
```java tab="Logging config with conversion"
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%replace(%xException){'\n','\u2028'}%nopex%n
logback-spring.xml
的示例配置:
在本例中,STDOUT
部分中的 pattern
属性被设置来记录日期、时间和日志消息,后面跟着一个换行符,如下所示。
通过在换行符前添加给定的代码片段,在异常消息中编码换行符。这将编码应用于异常的堆栈跟踪,但不修改其他日志事件。
```xml tab="Original logging config"
```xml tab="Logging config with conversion"
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%replace(%xException){'\n','\u2028'}%nopex%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
与 application.properties
相反,每个类定义XML/YAML配置。因此,必须为每个记录异常到 STDOUT
的类添加这个代码片段。任何支持格式 %replace(%xException){'\n','\u2028'}%nopex
的日志配置都可以相应地进行配置。
还有问题?
除非另行声明,该网站内容遵循MindSphere开发许可协议.