基本流程
- 先设计好完整需求(一开始系统不用过于复杂,预留优化点)
- 比如代码结构设计方面–抽出来新增加一个包
- 函数设计,把函数抽出来进行复用设计等
- 再先跑通流程,快速把代码完成,写测试类,流程能跑通再改细节(fast and dirty)
- 当想要引入某个技术时按流程来
- springboot引入
- 查看官方文档使用代码/直接询问ai有关对应需求的示例代码
- 在程序中以测试类跑通一遍
- 报错时先检查自己是否是一些简单的小问题(参数写错,注解用错,配置类未注入,引用依赖版本不适配(springboot版本过高)),确保自己没有问题再去思考别的方向的大错误。
- 最后把测试代码加入正式代码中,用debug模式启动查看运行情况
好习惯
- 以及常用sl4j添加注释
如何读错误信息和日志
示例:
nginx重启报错
:::info [root@iZbp1azwmv5wmtitf8tgi6Z ~]# service nginx restart Redirecting to /bin/systemctl restart nginx.service Job for nginx.service failed because the control process exited with error code. See “systemctl status nginx.service” and “journalctl -xe” for details.
[root@iZbp1azwmv5wmtitf8tgi6Z ~]# systemctl status nginx.service
● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Thu 2024-04-04 18:43:10 CST; 24s ago Docs: http://nginx.org/en/docs/ Process: 26937 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS) Process: 26943 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE) Main PID: 17811 (code=exited, status=0/SUCCESS) Apr 04 18:43:10 iZbp1azwmv5wmtitf8tgi6Z systemd[1]: Stopped nginx - high performance web server. Apr 04 18:43:10 iZbp1azwmv5wmtitf8tgi6Z systemd[1]: Starting nginx - high performance web server… Apr 04 18:43:10 iZbp1azwmv5wmtitf8tgi6Z nginx[26943]: nginx: [emerg] “log_format” directive is not allowed here in /etc/nginx/nginx.conf:35 Apr 04 18:43:10 iZbp1azwmv5wmtitf8tgi6Z systemd[1]: nginx.service: control process exited, code=exited status=1 Apr 04 18:43:10 iZbp1azwmv5wmtitf8tgi6Z systemd[1]: Failed to start nginx - high performance web server. Apr 04 18:43:10 iZbp1azwmv5wmtitf8tgi6Z systemd[1]: Unit nginx.service entered failed state. Apr 04 18:43:10 iZbp1azwmv5wmtitf8tgi6Z systemd[1]: nginx.service failed.
[root@iZbp1azwmv5wmtitf8tgi6Z ~]# journalctl -xe
– Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel – – Unit session-286.scope has finished starting up. – – The start-up result is done. Apr 04 18:43:35 iZbp1azwmv5wmtitf8tgi6Z sshd[26997]: pam_unix(sshd:session): session opened for user root by (uid=0) Apr 04 18:43:35 iZbp1azwmv5wmtitf8tgi6Z sshd[26997]: pam_unix(sshd:session): session closed for user root Apr 04 18:43:35 iZbp1azwmv5wmtitf8tgi6Z systemd-logind[558]: Removed session 286. – Subject: Session 286 has been terminated – Defined-By: systemd – Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel – Documentation: http://www.freedesktop.org/wiki/Software/systemd/multiseat – – A session with the ID 286 has been terminated. Apr 04 18:43:36 iZbp1azwmv5wmtitf8tgi6Z sshd[27006]: Accepted publickey for root from 118.31.243.186 port 9547 ssh2: RSA SHA256:Cu8WTxFKlUc86KGcNTGgs8QEj3NmGF6B/clY+k3lqCg Apr 04 18:43:36 iZbp1azwmv5wmtitf8tgi6Z systemd-logind[558]: New session 287 of user root. – Subject: A new session 287 has been created for user root – Defined-By: systemd – Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel – Documentation: http://www.freedesktop.org/wiki/Software/systemd/multiseat – – A new session with the ID 287 has been created for the user root. – – The leading process of the session is 27006. Apr 04 18:43:36 iZbp1azwmv5wmtitf8tgi6Z systemd[1]: Started Session 287 of user root. – Subject: Unit session-287.scope has finished start-up – Defined-By: systemd – Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel – – Unit session-287.scope has finished starting up. – – The start-up result is done. Apr 04 18:43:36 iZbp1azwmv5wmtitf8tgi6Z sshd[27006]: pam_unix(sshd:session): session opened for user root by (uid=0) Apr 04 18:43:36 iZbp1azwmv5wmtitf8tgi6Z sshd[27006]: pam_unix(sshd:session): session closed for user root Apr 04 18:43:36 iZbp1azwmv5wmtitf8tgi6Z systemd-logind[558]: Removed session 287. – Subject: Session 287 has been terminated – Defined-By: systemd – Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel – Documentation: http://www.freedesktop.org/wiki/Software/systemd/multiseat – – A session with the ID 287 has been terminated. Apr 04 18:44:16 iZbp1azwmv5wmtitf8tgi6Z postfix/master[1088]: warning: process /usr/libexec/postfix/pickup pid 27047 exit status 127 Apr 04 18:44:16 iZbp1azwmv5wmtitf8tgi6Z postfix/master[1088]: warning: /usr/libexec/postfix/pickup: bad command startup – throttling
:::
当解释日志报错信息时,关注的主要是以下几个方面:
- 报错级别和类型:日志中通常会指明错误的级别(如
emerg
、error
、warning
等)以及错误的类型(如nginx
、systemd
等),这有助于确定问题的严重程度和来源。 - 错误描述:日志中通常会提供对错误的简要描述,说明出现了什么问题。这些描述可能会指导我们解决问题的方向。
- 具体位置:日志通常会提供错误发生的具体位置,比如文件名和行号。这有助于我们快速定位到问题所在的代码或配置。
- 相关提示和建议:有时日志会提供额外的提示或建议,指导我们如何解决问题或者下一步该做什么。
针对你的问题,我注意到了以下关键信息:
- 报错级别:
emerg
,表示严重的紧急错误。 - 错误描述:指出了
nginx.conf
文件的第35行存在问题,具体是关于log_format
指令的使用错误。 - 具体位置:
nginx.conf
文件的路径和行号。 - 相关提示:提示了
log_format
指令在当前位置不被允许的问题。
通过这些信息,我能够分析出问题可能的原因,并提出相应的解决建议。
要学习如何解读日志报错信息,建议你首先熟悉常见的日志级别和类型,然后学会逐行分析日志,关注关键词和提示信息,理解错误的含义和出现的原因。随着经验的积累,你会越来越熟练地处理各种日志报错情况。